Elliptical orbit modeling
- by Nathon
I'm playing with orbits in a simple 2-d game where a ship flies around in space and is attracted to massive things. The ship's velocity is stored in a vector and acceleration is applied to it every frame as appropriate given Newton's law of universal gravitation. The point masses don't move (there's only 1 right now) so I would expect an elliptical orbit.
Instead, I see this:
I've tried with nearly circular orbits, and I've tried making the masses vastly different (a factor of a million) but I always get this rotated orbit.
Here's some (D) code, for context:
void accelerate(Vector delta)
{
velocity = velocity + delta; // Velocity is a member of the ship class.
}
// This function is called every frame with the fixed mass. It's a
// method of the ship's.
void fall(Well well)
{
// f=(m1 * m2)/(r**2)
// a=f/m
// Ship mass is 1, so a = f.
float mass = 1;
Vector delta = well.position - loc;
float rSquared = delta.magSquared;
float force = well.mass/rSquared;
accelerate(delta * force * mass);
}