Gravity around a point? (In 2D, like planets orbiting)
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?
This might be obvious but if you just want quick results.
http://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation
http://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation
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.
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.
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.
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|
Quote:Original post by Julian90Quote: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:Original post by NateTheGreatQuote:Original post by Julian90Quote: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
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;
The formulas are:
F = G*M*m/r^2;
a = F/m;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement