Jump to content
  • Advertisement
Sign in to follow this  

Law of gravitation

This topic is 3898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started writing my own game + engine a while ago. After a bit of struggling i managed to write a decent math library for my physics. And a today i finally started working on my physics engine. After going threw some basic physic formula's i strumpled across a problem. Newtons law of gravitation.. F = G * ((m1 * m2) / r) a1 = G * (m2 / r) G = 6.67e-11 The result of these formulas seem to be a float, however i figured using a three-dimensional vector would be more decent for accelerations and forces. So I was kind of wondering how I could alter this formula to result in a vector. Could I replace the distance (r) with a position1 - position2 vector, or would that corrupt the result?

Share this post

Link to post
Share on other sites
I'm fairly certain that r should be squared. You want gravity to have a larger impact as the objects are closer, and the impact should be smaller as they are farther away.

What is a1 in this system?

But you're a little bit off. The distance should be magnitude(position1 - position2). You need a vector, I'll call it d, which indicates the direction of the force. For your first body, d = position2 - position1 (a vector pointing to position2), and for your second, d = position1 - position2. You can multiply your force equation by this vector (normalize it first) to have your system work with vectors.

I don't know that what I'm telling you is strictly mathematically accurate (there are better physicists around here than me), but it worked fine in the game I made that used gravity. :-)


Share this post

Link to post
Share on other sites
Get the magnitude of the force through f = G*m1*m2/r2 and then multiply by the unitary/normalized vector that goes from body b1 to body b2 dirVec = (body2.position - body1.position)/|body2.position - body1.position|. And you finally get F = dirVec*(G*m1*m2/r2).


Share this post

Link to post
Share on other sites

public Vector3 GravitationalForce(Body body)
// Store distance
float r = position.Distance(body.Position);

// Return force
return (body.Position - this.position).Unit * (G * mass * body.Mass / (r * r));


Body earth = new Body("Earth");
earth.Mass = 5.98e24f;

Body jeroen = new Body("Jeroen");
jeroen.Mass = 70f;
jeroen.Position = new Engine.Math.Vector3(0, 6.39e6f, 0);

Console.WriteLine(earth.GravitationalForce(jeroen) + "N");

Results in a (0, 683.7909, 0) vector. Thats seems about right, thank you very much! :-)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!