Game Development Community

Need help with gravity

by Jereis Zaatri · in Torque Game Builder · 08/22/2010 (6:17 am) · 3 replies

Hello,

I need help figuring out why gravity is not working on my iTGB game. I've enabled physics and Gravity. I've also played with force scale. Is there something I need to do in code?

Also if I want to mock with the direction of gravity or have it be pulled by multiple objects, how do I go about it?

#1
08/22/2010 (9:48 pm)
Hey Jereis,

I accomplished basic "gravity" using the function SetConstantForceY(). I think I set the amount to 100, but it requires a good amount of tinkering to get the falling speed as you want it. It depends on all kinds of other physics factors, including the object size, its mass, and whether you are allowing Torque to "calculate" mass and inertia automatically (those can be set manually, but the results might be erratic).

Below is a very useful thread with a post by Melv May of Torque, describing the physics settings in greater detail; you can probably use this info, along with ConstantForceY(), as a starting point to achieving a nice realistic gravity effect.

http://www.torquepowered.com/community/forums/viewthread/95465

Note that you can use setConstantForceX() if you want "sideways" gravity. It can also be a polar value if you want directional/angular gravity, via the function:
setConstantForcePolar(%angle, %force, [%gravitic = false])

Beyond that, if you're looking for advanced gravitational effects like pushes and pulls emitted by various objects ("planets" for example"), that's a very complicated procedure as far as I know, and one that I have not yet tried to implement in TGB. If I had to ponder a solution to that, I might consider setting up circular triggers around the objects to define their gravitational range, then when objects enter that range they are pushed or pulled in relation to the emitting object using a much greater ConstantForcePolar() with an angle set to the relational angle between the two objects. Just a thought... I have no idea if that would really work, or how realistic it would look. :)

Best of luck!
#2
08/23/2010 (1:28 am)
I actually made a thread on trying to get gravitational effects. Currently objects can only be pulled through script, but I am trying to create it so that it works through code.

William Sims actually took my code and enabled particles to be attracted by gravity too!

Here's the link
#3
08/23/2010 (5:02 pm)
For anybody still following this thread, I considered another down-and-dirty method for applying gravity "pull" effects to objects, i.e. planets that might pull a spaceship toward them, or a magnet that might pull a metal object toward it. This would be done using the included mounting methods of TGB along with the MountForce setting.

In the following example, I'll just use "planet" for the pulling source and "spaceship" for the moving object that enters the planet's gravitational field. I haven't actually tried this in a game, but the theory is:

1) Set up a Trigger around the planet, probably a circular detection shape, and keep both OnEnterCallback and OnLeaveCallback enabled. Relate these objects to each other in whatever way you determine is best, such that the trigger knows which planet it's associated with.
2) Make sure that the planet has a mount linkpoint set at 0,0 (its center). You can add this later via script if you desire (see next step).
3) When the spaceship enters the trigger area, effectively the "pull field" of the planet, mount the spaceship to the planet. However, do not use a setting of "0" for the MountForce, because that will instantly snap the spaceship to the planet's 0,0 linkpoint. Instead, use a higher value (0.5 or greater?) which should gradually pull the spaceship toward the linkpoint as if it's attached by an elastic string.
4) If the spaceship manages to leave the gravitational field (trigger) via player control or possibly by its own volition (i.e. it's moving very fast and manages to leave the trigger area before touching the planet), an OnLeaveCallback() could simply dismount the spaceship, and it would continue on its way.
5) Simple collision settings would allow you to perform an action if the spaceship touches the planet, i.e. if the spaceship hits the surface of a fiery planet, it burns up and is destroyed.

Anyway, that's the theory. It might not work in practice, but it seems logical to me. Instead of calculating a whole slew of angles and ConstantForce settings, as I hinted in my post above, this uses TGB's built-in mounting methods and MountForce setting.

Any comments or suggestions? Please let me know, since I'll probably be attempting to get this behavior functional in an upcoming game idea. Improvements or ideas are very welcome.