# Gravity around a point? (In 2D, like planets orbiting)

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

## Recommended Posts

I've been trying to do this today but I keep getting awkward results. Does anyone know of a good way to calculate gravity around a point, like planets orbiting a star?

##### Share on other sites
This might be obvious but if you just want quick results.

http://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation

##### Share on other sites
Quote:
 Original post by SirisianThis might be obvious but if you just want quick results.http://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation

Yeah I was using that but I was mostly having problems putting it into x and y coords. I could only figure out the hypotenuse of the force, and had trouble converting it into x and y.

##### Share on other sites
It's been a while since I wrote a planet simulation but here.

Vector directionOfForce = object1.position - object2.position;

//radius = Math.Sqrt((object1.x - object2.x)*(object1.x - object2.x) + (object1.y - object2.y) * (object1.y - object2.y));

//normalize the direction
directionOfForce.Normalize();

Once you understand that then:

float gConstant = 1;

for(int i = 0; i < AllObjects.Count; ++i){
for(int j = 0; j < AllObjects.Count; ++j){
if(i != j){
Vector directionOfForce = AllObjects.position - AllObjects[j].position;
directionOfForce.Normalize();
}
}
}

I think that's right. Try it.

Basically for every object apply the force of every other object in relations to it's mass.

##### Share on other sites
Given a radius r and an angle θ:
x = r cos θy = r sin θ

##### Share on other sites
Quote:
 Original post by OluseyiGiven a radius r and an angle θ:x = r cos θy = r sin θ

Useful if you have the angle, but you don't.

You probably want:

F = G * m1 * m2 / r3 * r

where r = p2 - p1 and r = |r|

##### Share on other sites
Quote:
Original post by Julian90
Quote:
 Original post by OluseyiGiven a radius r and an angle θ:x = r cos θy = r sin θ

Useful if you have the angle, but you don't.

You probably want:

F = G * m1 * m2 / r3 * r

where r = p2 - p1 and r = |r|

But then how do I get that into x and y?

##### Share on other sites
Quote:
Original post by NateTheGreat
Quote:
Original post by Julian90
Quote:
 Original post by OluseyiGiven a radius r and an angle θ:x = r cos θy = r sin θ

Useful if you have the angle, but you don't.

You probably want:

F = G * m1 * m2 / r3 * r

where r = p2 - p1 and r = |r|

But then how do I get that into x and y?

Quote:
 where r = p2 - p1

that is how. p2 and p1 have both x, and y components. breaking it up it goes like this

rX = pX2 - pX1
rY = pY2 - pY1

FX = G * m1 * m2 / r3 * rX
FY = G * m1 * m2 / r3 * rY

##### Share on other sites
Thanks everyone, I got it working now :)

##### Share on other sites
Where did you guys get a formula for Newtonian gravitation with r^3 terms in it?

The formulas are:

F = G*M*m/r^2;
a = F/m;

• 17
• 11
• 14
• 9
• 49
• ### Forum Statistics

• Total Topics
631393
• Total Posts
2999763
×