Game Development Community

Gravity Wells and Black Holes

by Russell Tracey · in Torque Game Builder · 03/31/2005 (4:52 pm) · 44 replies

I'm attempting to produce a gravity well type effect which sucks objects towards it in a black hole like way. I've tried a mounting an object to the centre of the black hole and setting it to chase the mount point but this doesn't seem to work. I have tried several different force values but they are either too weak or too strong. If its too much then the objects go straight to the centre, if its not enough they fly right past but gradually slow until they are stop half way off the screen.

What I am trying to achieve is a black hole that will start to drag a ship into it if the ship gets too close, if the ship however has enough force it can escape the black hole but its direction will be altered slightly due to the effects of the black hole.

Has anyone got something like this working?
#21
04/05/2005 (8:39 am)
@Melv: I'd be curious to see it just to see it. You can send it to drakki at rollanet.org
#22
04/05/2005 (8:48 am)
@Melv: any chance I could sneak in here and get an e-mail of the code you were talking about :)?
#23
04/05/2005 (10:07 am)
Emails sent Tim/Matthew.

@Alex: Maybe in one form or another but the code I'm sending out here won't be.

- Melv.
#24
04/10/2005 (10:22 am)
I'd like to see the Melv's majik grav code as well - joshqpublic@gmail.com
#25
04/10/2005 (10:55 am)
Email Sent. :)

- Melv.
#26
06/05/2005 (10:08 am)
Hey Melv,
I'd be interested in taking a peek at what you wrote just to see how you did it (since you wrote the bloody engine and all), it sounds like it might be a good basis for some custom work I need to do for my project (hopefully I can let the cat out of the bag when I get it prototyped).

sanguinus@earthlink.net

Thanks Melv.
#27
06/06/2005 (10:08 am)
Jeff,

Email Sent. :)

- Melv.
#28
07/28/2005 (2:01 pm)
Melv,
I'd be interested in the grav code you emailed to everyone else, as well.
Original Spacewar remix, here we come. :)

EDIT: Oops, email. mharpold8 at (NOT THIS) comcast.net
#29
07/28/2005 (4:54 pm)
Melv: Maybe the grav code could be added to TDN or posted as a t2d resource if you want to keep it out of the main engine.

BTW I'd like to see the code :) I did implement a gravity well for my last GID, but I did it in script using polarvelocity each scene update. Worked pretty well even though it was a hack :P, but I always like to look over alternative methods :)
#30
07/29/2005 (12:45 am)
I'll dig the email out when I get home. Please bear in mind that it may not still be compatible with T2D without modification and definately not with the next release.

- Melv.
#31
07/29/2005 (7:35 pm)
As I recall, there was some formula floating around to compute the position of an object after gravity is integrated into it's location and speed, but I have no idea what that was.

But I think you guys have it covered : )
#32
07/30/2005 (12:44 am)
Emails sent.

- Melv.
#33
09/20/2005 (6:20 pm)
I'd like to see this code as well...email is zenchessnews@gmail.com
#34
09/21/2005 (11:37 am)
To save me continually posting this, I've placed it in an "unsupported" folder on the GG public server here.

Enjoy!

- Melv.
#35
09/21/2005 (5:26 pm)
Somewhat related to this I needed to have "forces" for the Bobbles thing I'm working on and I did so, but not using this method. Instead what I did was to use sceneobjects mounted to the sprite and use onCollision() to handle the attraction and repulsion by setting impulse forces aligned to the collision normal.

Being all script there are advantages and disadvantages, but you definitely can get the gravitational slingshot effect so there is sufficient accuracy for that. The forces in Bobbles don't vary with distance, but (other than performance issues) that would be easy enough to account for as well.

Note: there *are* accuracy issues. If you use the same start point and repeatedly run a simulation you will get varying results. But it is quick (to code) and precise enough for simple games.

Any thoughts as to whether or not this is better (performance-wise) than a script-implemented schedule? I'm thinking it depends on the situation (e.g., since force ranges are limited it doesn't have to iterate over every object to check for force interaction with every other object), but maybe it falls down due to constant collisions (of nearby objects)?
#36
06/21/2006 (9:38 am)
Does the code work with the current version of TGB (RC3)?

Please consider adding gravity objects or attributs to the release version!
#37
06/25/2006 (6:44 pm)
If anyone is curious how to get a similar effect without modifying the source, here is a simple example I just threw together for a trigger object. If you want to play with it, just make a trigger, turn off its physics, set its collision detection to CIRCLE, and set its class to "magnet". The script works off the onStay callback for triggers (which you have to enable). Here it is:

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


function magnet::onStay(%this, %obj)
{   
   %forceScale = 100;
   %baseVec = t2dVectorSub(%this.getPosition(),%obj.getPosition());
   if(t2dVectorLength(%baseVec) < 1) {
      %magnitude = %forceScale;
   } else {
      %magnitude = %forceScale/t2dVectorLength(%baseVec);
   }
   %impulseForce = t2dVectorScale(t2dVectorNormalise(%baseVec), %magnitude);
   %obj.setImpulseForce(%impulseForce, true);
}

Edit: Spelling...
#38
06/25/2006 (6:51 pm)
By the by, if you wanted to get a repulsion effect rather than the attraction in the above example, all you would have to do is reverse the order of the parameters in t2dVectorSub and it should work. That line would look like the following:

...
   %baseVec = t2dVectorSub(%obj.getPosition(),%this.getPosition());
...
#39
06/25/2006 (7:01 pm)
Also, just a side note: I'm not positive on this, but I think by default objects only handle one collision per frame, so you might need to modify the source if you wanted multiple objects affected by the same 'magnetic field' at the same time.
#40
07/07/2006 (7:53 pm)
Thomas, pardon my ignorance, but how do you set up a trigger? Is that just a sprite at the same location as the planet/blachkhole, but an invisible one?