I'm having serious troubles with getting my calculations to work. I'm calculating the orbital elements from a position vector, and a velocity vector. (I also know the distance and mass of the bodies around)

The equation for angular momentum is as follows: h=r*v

The equation for eccentricity is: e=((v^2−μ/|r|)r-(r⋅|v|)v/µ

I plug in the position and velocity to get h, and I plug them into the other equation to get the eccentricity. These SHOULD be constants, and not change unless another force is put on them... yet they are not constant. In fact they change as the orbit goes around.


In the diagram I have just left a trail to show the orbital path. I should be able to calculate the required information... however they rely on the constants, which are not at all constant. I reference the rigidbody's position and velocity in them, and I've entered the equations in correctly. My code for applying force between planets is as follows:

void CalculateForce(Transform body) {
    float m1 = rbody.mass;
    float m2 = body.GetComponent<Rigidbody> ().mass;
    float distance = Vector3.Distance (transform.position, body.position);
    float f = (m1 * m2) / Mathf.Pow (distance, 2);
    Vector3 direction = rbody.position - body.GetComponent<Rigidbody> ().position;
    rbody.AddForce (-direction.normalized * f);
    body.GetComponent<Rigidbody> ().AddForce (direction.normalized * f);

The calculations are coming back incorrect. The eccentricity varies between 0.9 and 1.1. Anything over a 1 is not actually in orbit, and anything less is in orbit. However this remains in orbit as it travels around.

Can anybody help? (I've already posted to the space stack exchange and had no luck)

Thanks in advance.


Code used to calculate eccentricity (1001f is the combined weight of both objects):

    Vector3 pos = GetComponent <Rigidbody2D> ().position;
    Vector3 vel = GetComponent <Rigidbody2D> ().velocity; 
    Vector3 eccentricity = (Mathf.Pow (vel.magnitude, 2.0f) - (1001f / pos.magnitude)) * pos;
    eccentricity -= Vector3.Dot (pos, vel) * vel;
    eccentricity /= 1001f;
  • 1
    \$\begingroup\$ Sounds like inaccuracies are piling up due to the physics integration. In the past, when I've needed to simulate orbits accurately, I've driven them straight off of Kepler's equations (ie. each frame I plug in the current time parameter, and solve for the position of the body at that moment). This is a little tricky since it's non-linear (calculating the anomalies is iterative), but ensures the object stays on the correct orbit forever. Would something like this be an option in your case? \$\endgroup\$ – DMGregory Jul 9 '15 at 23:48
  • \$\begingroup\$ It could well be the cause. I've started a fresh project, and it's looking to give me a constant now. I'll look into putting the Kepler's equations in, if it doesn't work. \$\endgroup\$ – Jacob Morris Jul 9 '15 at 23:58
  • \$\begingroup\$ @JacobM You might try changing float f = (m1 * m2) / Mathf.Pow (distance, 2); to float f = (m1 * m2) / Mathf.Pow (distance, 2.0f); Sometimes error can come from doing operations between integers and floats.(had the hardest time trying to code block in comment.. guess its not possible) \$\endgroup\$ – Stephan Jul 10 '15 at 15:41
  • \$\begingroup\$ I've tried changing the 2 to 2.0f at all occurrences, but it appears to have no difference. I've updated my post with the code used to calculate eccentricity, if it helps. \$\endgroup\$ – Jacob Morris Jul 10 '15 at 16:47
  • \$\begingroup\$ @Stephan Mathf.Pow() only takes floats, so a 2 is implicitly cast to a float. \$\endgroup\$ – DMGregory Jul 10 '15 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.