// Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         // toggle the engine
         if (rocketEngine.engineOn)
         {
             rocketEngine.SetEngine(false);
             ge.BodyOnRails(shipNbody, centerNbody);
         }
         else
         {
             rocketEngine.SetEngine(true);
             Vector3d vel = ge.GetVelocityDoubleV3(shipNbody);
             rocketEngine.SetThrustAxis(vel.ToVector3().normalized);
             ge.BodyOffRails(shipNbody, ge.GetPositionDoubleV3(shipNbody), vel);
         }
     }
 }
    public void Evolve(double physicsTime, ref double[] r)
    {
        if ((activeElement < keplerElements.Count - 1) &&
            (physicsTime > keplerElements[activeElement + 1].timeStart))
        {
            // Advance to next element
            activeElement++;
            GravityEngine ge       = GravityEngine.Instance();
            KeplerElement activeKE = keplerElements[activeElement];
            if (activeKE.returnToGE)
            {
#pragma warning disable 162     // disable unreachable code warning
                if (GravityEngine.DEBUG)
                {
                    Debug.Log("return to GE:" + gameObject.name);
                }
#pragma warning restore 162
                KeplerElement priorKE = keplerElements[activeElement - 1];
                priorKE.orbit.Evolve(physicsTime, ref r);
                Vector3d pos = priorKE.orbit.GetPositionDouble();
                Vector3d vel = priorKE.orbit.GetVelocityDouble();
                ge.BodyOffRails(nbody, pos, vel);
                return;
            }
            else
            {
                // if the center object of the orbit changes, need to recompute the KeplerDepth and update
                if (keplerElements[activeElement - 1].orbit.centerNbody != activeKE.orbit.centerNbody)
                {
                    NewCenter(activeKE.orbit);
                }
                // move on to the next orbit in the sequence
                activeKE.orbit.PreEvolve(ge.physToWorldFactor, ge.massScale);
                if (activeKE.callback != null)
                {
                    activeKE.callback(activeKE.orbit);
                }
                if ((activeKE.maneuver != null) && (activeKE.maneuver.onExecuted != null))
                {
                    activeKE.maneuver.onExecuted(activeKE.maneuver);
                }
            }
#pragma warning disable 162     // disable unreachable code warning
            if (GravityEngine.DEBUG)
            {
                Debug.LogFormat("Changed to segment {0} tnow={1} tseg={2} ", activeElement, physicsTime,
                                keplerElements[activeElement].timeStart);
            }
#pragma warning restore 162
        }
        else if (physicsTime < keplerElements[activeElement].timeStart)
        {
            // Use an earlier element (happens if time set to earlier)
            int lastElement = activeElement;
            while (physicsTime < keplerElements[activeElement].timeStart && (activeElement >= 0))
            {
                activeElement--;
                // if the center object of the orbit changes, need to recompute the KeplerDepth and update
                if (keplerElements[lastElement].orbit.centerNbody != keplerElements[activeElement].orbit.centerNbody)
                {
                    NewCenter(keplerElements[activeElement].orbit);
                }
            }
        }

        if (!keplerElements[activeElement].returnToGE)
        {
            // time for evolve is absolute - up to OrbitUniversal to make it relative to their time0
            keplerElements[activeElement].orbit.Evolve(physicsTime, ref r);
        }
    }