Game Development Community

Simulating Attraction between objects in space

by Maurice Ribble · in Torque Game Builder · 12/17/2006 (5:38 am) · 3 replies

I'm new to TGB so this might be an answered question. If it is just point me to the right resource (didn't find it when searching, but I probably didn't search for the right things).

What I want to do is simulate the gravitational attractive forces between things like the earth and the sun. However, I'm going to use is between lots of objects (ie for certain projectiles, between ships and black holes, ...). I avoided the word gravity in the subject, but it seems TGB's gravity is a linear force and is not suited for this. Does anyone have any ideas on how I would do something like this? I have a few ideas, but none of them seem very good.

There seems to be two issues. One is first detecting when one object is close enough to another to start having it apply gravity. It would seem good to use the collision system in TGB for this, but I don't see how to extend the collision detection beyond the sprite object (is there a common tricky way to do this?). I could use a list of all objects and manually check some of the objects in this list against other ones each frame, but that could get slow for lots of objects on a big level. I could add something like an Oct tree, but then it feels like I'm doing things that TGB should be handling.

Problem two is actually applying the attractive force. The physics function descriptions in the reference guide are a little sparse. I'd have to do a bunch of experimenting to find the right one so if someone knows what functions I should use that would be great.

Thanks for any help!

#1
12/17/2006 (10:00 am)
Here's one good thread and here's another.

The second one is much older, but has some nice insights.

Found 'm both just reading the Scripting and Physics Forums Areas ;)
#2
12/17/2006 (12:43 pm)
Both of your links were to the same post. I found this one, which is probably the second one you wanted. I think I have an idea on how to do it. It doesn't seem like it will be great performance, but it should work for testing. If I need something faster I'll revisit that part of the problem later.
#3
12/17/2006 (3:07 pm)
It turned out applying gravity to areas (even large areas) is pretty easy to do. I just created a trigger. The trigger allows me to extend the gravity way beyond my black hole sprite. Then I took some script code from Thomas Buscaglia and modified it slightly that roughly estimates gravity. Lots of tweaking is needed to get good results, but the basics are working now.

function GravityWell::onLevelLoaded( %this, %scenegraph )
{
   %this.setStayCallback(true);
}

function GravityWell::onStay(%this, %obj)
{   
   %forceScale = 300;
   %baseVec = t2dVectorSub(%this.getPosition(),%obj.getPosition());
   if(t2dVectorLength(%baseVec) < 1)
   {
      %magnitude = %forceScale;
   }
   else
   {
      %magnitude = %forceScale/t2dVectorLength(%baseVec);
   }
   %constForce = t2dVectorScale(t2dVectorNormalise(%baseVec), %magnitude);
   %obj.setConstantForce(%constForce, true);
}