Mass Based Gravity

Topics: Developer Forum, User Forum
Apr 16, 2010 at 6:52 PM
Edited Apr 16, 2010 at 7:13 PM

I'm trying to apply mass based gravity in my game doing:


                    float gravity = 0;
for (int i = 0; i < Global.PhysicsSimulator.BodyList.Count; i++)
if (Global.PhysicsSimulator.BodyList[i].isStatic)
gravity = 0;
for (int j = 0; j < Global.PhysicsSimulator.BodyList.Count; j++)
gravity += (100.0f * (Global.PhysicsSimulator.BodyList[i].mass * Global.PhysicsSimulator.BodyList[j].mass)) / (float)Math.Pow(ExMath.Range(Global.PhysicsSimulator.BodyList[i].Position, Global.PhysicsSimulator.BodyList[j].Position), 2);
Global.PhysicsSimulator.BodyList[i].ApplyForce(new Vector2(0, gravity));

But the angular velocity of an object becomes NaN. Any ideas or better way i can do this?

Looking in to gravity controllers now.


Apr 17, 2010 at 11:23 PM

You'll have problems with that when the bodies get close together...

Also, it looks like you're calculating gravity between two bodies, but then applying the force downwards. Shouldn't you be applying force in the direction of the body that's pulling?