Jump to content
  • Advertisement
Sign in to follow this  
NateTheGreat

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.

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

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 this post


Link to post
Share on other sites
Advertisement
This might be obvious but if you just want quick results.

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

Share this post


Link to post
Share on other sites
Quote:
Original post by Sirisian
This 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 this post


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


Vector directionOfForce = object1.position - object2.position;

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

//normalize the direction
directionOfForce.Normalize();
//directionOfForce.x /= radius;
//directionOfForce.y /= radius;

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;
float radius = directionOfForce.Length();
directionOfForce.Normalize();
AllObjects.velocity.x += gConstant * directionOfForce.x * AllObjects[j].mass / (radius * radius);
AllObjects.velocity.y += gConstant * directionOfForce.y * AllObjects[j].mass / (radius * radius);
}
}
}

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 this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Given 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 this post


Link to post
Share on other sites
Quote:
Original post by Julian90
Quote:
Original post by Oluseyi
Given 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 this post


Link to post
Share on other sites
Quote:
Original post by NateTheGreat
Quote:
Original post by Julian90
Quote:
Original post by Oluseyi
Given 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 this post


Link to post
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;

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!