Elliptical orbit modeling

Posted by Nathon on Game Development See other posts from Game Development or by Nathon
Published on 2012-06-22T15:59:04Z Indexed on 2012/06/22 21:26 UTC
Read the original article Hit count: 266

Filed under:
|
|

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:

This is what I see

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

© Game Development or respective owner

Related posts about math

Related posts about physics