Approximating gravitational interactions
by David Ravel · in Torque Game Builder · 10/11/2009 (6:32 pm) · 26 replies
Hi,
I'm hoping to make a small game featuring objects interacting along a flat plane, exerting forces on each other similar to gravitational effects. I want these objects (a dozen moving on and off the screen would be ideal) to exert these forces on each other, changing their relative velocities, and falling into things resembling orbits around each other. I'm not aiming for accuracy here, and I would plan on exaggerating and approximating the results of these effects quite a bit for game play's sake. To keep things from becoming static once everything settled into a stable spatial relationship I'd have an additional force manipulating these object, an additionally ability to effect changes of velocity. The player's object (he would just be one of these many) would possess this ability to change his velocity and move against the grain of the influence of mass. Other objects would make use of this ability on occasion to move themselves off in different directions.
The analogy at work here is that the objects represent humans in their movement through the social sphere. The gravity exerted by their mass would represent their natural, unconscious movements and tendencies. The ability to exert some additional force than this, to be able to move independently (the object would still be under gravities effects, offset by this movement.) would more represent conscious movement. There would probably be an additional kind of force interacting between the objects, that of attraction and repulsion based on some other property other than mass, a kind of preference or distaste for objects different types. As far as complexity of interaction, this seems to contain a good deal, and I'm not sure how well these objects would interact with each other, but I need to at least get the system operational to determine that.
I'm not really sure how to proceed, as I am a game designer and scripter, and although I am familiar with TGB a bit I'm not enough of a programmer to be able to figure this out on my own. I was hoping if anybody could point me in the right direction so I could at least communicate intelligently while finding a programmer to contract this out to.
Thanks a lot for any help you guys can offer,
Dave
I'm hoping to make a small game featuring objects interacting along a flat plane, exerting forces on each other similar to gravitational effects. I want these objects (a dozen moving on and off the screen would be ideal) to exert these forces on each other, changing their relative velocities, and falling into things resembling orbits around each other. I'm not aiming for accuracy here, and I would plan on exaggerating and approximating the results of these effects quite a bit for game play's sake. To keep things from becoming static once everything settled into a stable spatial relationship I'd have an additional force manipulating these object, an additionally ability to effect changes of velocity. The player's object (he would just be one of these many) would possess this ability to change his velocity and move against the grain of the influence of mass. Other objects would make use of this ability on occasion to move themselves off in different directions.
The analogy at work here is that the objects represent humans in their movement through the social sphere. The gravity exerted by their mass would represent their natural, unconscious movements and tendencies. The ability to exert some additional force than this, to be able to move independently (the object would still be under gravities effects, offset by this movement.) would more represent conscious movement. There would probably be an additional kind of force interacting between the objects, that of attraction and repulsion based on some other property other than mass, a kind of preference or distaste for objects different types. As far as complexity of interaction, this seems to contain a good deal, and I'm not sure how well these objects would interact with each other, but I need to at least get the system operational to determine that.
I'm not really sure how to proceed, as I am a game designer and scripter, and although I am familiar with TGB a bit I'm not enough of a programmer to be able to figure this out on my own. I was hoping if anybody could point me in the right direction so I could at least communicate intelligently while finding a programmer to contract this out to.
Thanks a lot for any help you guys can offer,
Dave
#22
I've got the function running, but the three objects that are currently being subjected to it (and who have positions setup a bit away from each other) seem to all start at position "0 0" and never change. Is this resulting from the early line that sets the acceleration to "0 0"? I thought that was supposed to be how it was setup, so we could calculate everything from scratch. At first I thought some of the constituent settings were too high and they were just coalescing together but they seem to start that way at the "0 0" origin. I took a shot in the dark and tried to set the acceleration to whatever their position was at the time of the function being called, but that didn't seem to do anything.
I know you guys are getting tired of wasting all your time in this thread, but I really appreciate your efforts.
10/16/2009 (4:50 pm)
Ok, I think I've got an intelligent question.I've got the function running, but the three objects that are currently being subjected to it (and who have positions setup a bit away from each other) seem to all start at position "0 0" and never change. Is this resulting from the early line that sets the acceleration to "0 0"? I thought that was supposed to be how it was setup, so we could calculate everything from scratch. At first I thought some of the constituent settings were too high and they were just coalescing together but they seem to start that way at the "0 0" origin. I took a shot in the dark and tried to set the acceleration to whatever their position was at the time of the function being called, but that didn't seem to do anything.
function doTick(%n, %m, %obj, %otherObj, %distance, %vec, %forceMagnitude, %vecNorm, %acc, %G, %dt)
{
// dt is the amount of simulated time that has passed since the last tick.
// if you want greater accuracy, make dt smaller, and call doTick() multiple times per frame.
// calculate the accelerations on each object. this is the O(n^2) part, but don't sweat it.
for (%n = Bodies.getCount() - 1; %n >= 0; %n--)
{
%obj = Bodies.getObject(%n);
%obj.acceleration = "0 0";
for (%m = Bodies.getCount() - 1; %m >= 0; %m--) //Should this be the same as %n?
{
if (%m != %n)
{
%otherObj = Bodies.getObject(%m);
%vec = vectorSub(%otherObj.position, %obj.position);
%dt = "1";
%G = "9.8"; // gravity constant. tweak this as you see fit to make things work out nicely.
%distance = VectorLen(%vec);
%forceMagnitude = %G * %obj.mass * %otherObj.mass / (%distance * %distance);
%vecNorm = vectorScale(%vec , 1.0 / %distance); // unit-length vector
%acc = vectorScale(%vecNorm, %forceMagnitude);
%obj.acceleration = vectorAdd(%obj.acceleration, %acc);
}
}
}
// swell, now calculate the velocities of each object.
for (%n = Bodies.getCount() - 1; %n >= 0; %n--)
{
%obj = Bodies.getobject(%n);
%obj.velocity += %obj.acceleration * %dt;
}
// and finally the positions
for (%n = Bodies.getCount() - 1; %n >= 0; %n--)
{
%obj = Bodies.getobject(%n);
%obj.position += %obj.velocity;
echo (%obj.getposition()); //So I can check to see if anything's actually happening to the objects
}
schedule(50, 0, doTick);
}I know you guys are getting tired of wasting all your time in this thread, but I really appreciate your efforts.
#23
Also, your "doTick" function doesn't need all of those parameters there at the top.
I see you have an "echo". Is your console printing out the positions of the objects?
I don't want to look into this, but I'm certain the 2 lines with "+=" on them won't work. You'll have to change them to vectorAdd's, as in "%obj.velocity = t2dVectorAdd( %obj.velocity, t2dVectorScale( %obj.acceleration, %dt ) );".
And I guess that brings up the point that your vector functions are mis-named.
10/16/2009 (4:58 pm)
Do you call "doTick" a first time from somewhere?Also, your "doTick" function doesn't need all of those parameters there at the top.
I see you have an "echo". Is your console printing out the positions of the objects?
I don't want to look into this, but I'm certain the 2 lines with "+=" on them won't work. You'll have to change them to vectorAdd's, as in "%obj.velocity = t2dVectorAdd( %obj.velocity, t2dVectorScale( %obj.acceleration, %dt ) );".
And I guess that brings up the point that your vector functions are mis-named.
#24
ahh, that makes sense. I was looking at the vector stuff, but in Torsion they came up in that blueish color that usually means it's recognized and working, but I'll see if changing them does anything.
I was kind of confused by the += stuff, because I couldn't find any reference to that symbol in tdn. I'll go through and work on making the changes.
Thanks for the pointers.
10/16/2009 (5:28 pm)
yeah the function get's called straight from the game.cs after the level loads and executes all the scripts. My console is printing out he positions quite rapidly, that was how I was able to determine that something was going wrong from the beginning.ahh, that makes sense. I was looking at the vector stuff, but in Torsion they came up in that blueish color that usually means it's recognized and working, but I'll see if changing them does anything.
I was kind of confused by the += stuff, because I couldn't find any reference to that symbol in tdn. I'll go through and work on making the changes.
Thanks for the pointers.
#25
Thank you so much guys!
EDIT: Messing with the falloff definitely did the trick, Orion.
10/16/2009 (7:25 pm)
Aha! It works, thanks to the both of you! It's behaving exactly as I imagined. Oh man, this is great!Thank you so much guys!
EDIT: Messing with the falloff definitely did the trick, Orion.
#26
10/16/2009 (9:58 pm)
You're definitely welcome! I hope your idea goes well!
Torque Owner David Ravel
I bit off a bit more than I could chew with this but it's been a really good learning experience being guided through it. I figure that once this interaction is working I'll be back to dealing with stuff I can more easily find in the books, tutorials, and reference docs.
Thanks a ton to both of you guys for helping me with this one, I really appreciate it. I'll definitely put time in and brush up on my skills so that next time I come by with a question I've got a somewhat decent foundation to work off of.