1
\$\begingroup\$

I'm playing around and trying to build an orbit simulator. Unfortunately, it isn't working out to well. Instead of forming some sort of elliptical orbit, I get this:

my orbit

Here is the code I'm using to generate the orbits:

public class OrbitBehaviour : MonoBehaviour {
    public Double Mass;
    private List<GameObject> _bodies = new List<GameObject>();
    private float _gravConstant = 6.673f * Mathf.Pow(10.0f, -11.0f); // Units: m3 kg-1 s-2
    private int delay = 0;
    private float _distanceConversion = 6.68458712f * Mathf.Pow(10.0f, -12.0f); // m -> AU (Astronomical Units)
    private float _massConversion = 1.67403241f * Mathf.Pow(10.0f, -25.0f); // kg -> E (Earth masses)
    private float _timeConversion = 1.1574074074074074074074074074074e-5f; // s -> D (Days)

    // Use this for initialization
    void Start() {
        _bodies = GameObject.FindGameObjectsWithTag("mass").ToList();
        _gravConstant *= Mathf.Pow(_distanceConversion, 3.0f);
        _gravConstant /= _massConversion;
        _gravConstant /= Mathf.Pow(_timeConversion, 2.0f);
        gameObject.GetComponent<Rigidbody>().AddForce(Vector3.left * 10f);
        Mass = Mass * _massConversion;
        Time.timeScale = 99.0f;
    }

    // Update is called once per frame
    void FixedUpdate() { StepSimulation(); }

    private void StepSimulation() {
        // Walk each body in the system
        foreach (GameObject body in _bodies) {
            if (gameObject == body) {
                continue; //skip self
            }
            Vector3 tSeparationVector = body.transform.position - gameObject.transform.position;
            tSeparationVector = tSeparationVector.normalized;
            OrbitBehaviour bodyBehaviour = body.GetComponent<OrbitBehaviour>();
            Double tForceValue = _gravConstant * (this.Mass * bodyBehaviour.Mass / tSeparationVector.sqrMagnitude);
            gameObject.GetComponent<Rigidbody>().AddForce(tSeparationVector.normalized * (float)tForceValue);
        }
    }
}

Does anyone know what I am doing wrong here?

\$\endgroup\$
  • \$\begingroup\$ Change all your floats to doubles. That will reduce the magnitude of the behaviour significantly, but the true origin is that you are assuming perfect integration of finite-precision numbers, and the rounding errors are accumulating. \$\endgroup\$ – Pieter Geerkens Jul 16 '15 at 2:12
  • \$\begingroup\$ it doesn't seem that you have access to the integrator ? can you change it to runge kutta 2 ? Euler integration cannot give you a consistent ellipse. \$\endgroup\$ – v.oddou Jul 16 '15 at 4:52
  • \$\begingroup\$ @v.oddou Unity's integrator is not extensible. It gives you only an interpolation property that'll take None, Interpolate, or Extrapolate. My guess is that they're all euler, but I can't find anything in the Unity API confirming that, nor anything in the PhysX SDK with the same naming. Unity API here: docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html \$\endgroup\$ – Slipp D. Thompson Dec 15 '15 at 21:50
  • \$\begingroup\$ here gamedev.stackexchange.com/questions/79938/… is written what Unity uses, he says it's an in-house improved symplectic integrator. Which I don't know what to say about. \$\endgroup\$ – v.oddou Dec 16 '15 at 0:24

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.