# 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.

## 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 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. :-)

Cheers,
--Brian

##### 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).

HTH

##### Share on other sites
Method:

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));
}

Implementation:

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! :-)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 19
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633382
• Total Posts
3011581
• ### Who's Online (See full list)

There are no registered users currently online

×