Example #1
0
    /// <summary>
    /// Mark intercepts with the designated symbols and return a list of intercepts found
    /// for the predicted path of ship intersecting with the path of the target.
    /// </summary>
    ///
    /// <param name="ship">The ship</param>
    /// <param name="target">The target</param>
    ///
    /// <returns>The intercepts.</returns>
    private List <TrajectoryData.Intercept> MarkIntercepts(SpaceshipRV ship, NBody target)
    {
        // delta distance is scale dependent. For now use an ugly *if*
        float deltaDistance = 1f;

        if (GravityEngine.Instance().units == GravityScaler.Units.ORBITAL)
        {
            deltaDistance = 20f;
        }
        const float deltaTime    = 2f;
        const float rendezvousDT = 1f;

        trajIntercepts.spaceship = ship.GetTrajectory();
        Trajectory trajectory = target.GetComponentInChildren <Trajectory>();

        if (trajectory == null)
        {
            Debug.LogError("Target requires a child with a trajectory component");
            return(new List <TrajectoryData.Intercept>());
        }
        trajIntercepts.target = trajectory;
        trajIntercepts.ComputeAndMarkIntercepts(deltaDistance, deltaTime, rendezvousDT);
        intercepts = trajIntercepts.GetIntercepts();
        return(intercepts);
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if (nbody == null)
        {
            return;             // misconfigured
        }
        Vector3 thrust          = axisN;
        bool    rotationChanged = false;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            running = !running;
            if (running && thrustSize > 0)
            {
                thrust = thrustSize * axisN;
                thrust = transform.rotation * thrust;
                GravityEngine.instance.ApplyImpulse(nbody, thrust);
                // reset thrust
                thrustSize = 0f;
                SetThrustCone(thrustSize);
            }
            else
            {
                // into not running:
                // - get the spaceship velocity
                shipVelocity = GravityEngine.instance.GetVelocity(transform.parent.gameObject);
                shipPosition = transform.parent.position;
            }
            GravityEngine.instance.SetEvolve(running);
        }
        else if (Input.GetKey(KeyCode.F))
        {
            thrust = thrustPerKeypress * axisN;
            thrust = transform.rotation * thrust;
            GravityEngine.instance.ApplyImpulse(nbody, thrust);
        }
        else if (Input.GetKeyDown(KeyCode.R))
        {
            // debug code to assess cost of restart
            GravityEngine.instance.TrajectoryRestart();
        }
        else if (Input.GetKeyDown(KeyCode.T))
        {
            // toggle trajectory prediction
            bool trajPrediction = GravityEngine.instance.trajectoryPrediction;
            GravityEngine.instance.SetTrajectoryPrediction(!trajPrediction);
        }
        else if (Input.GetKeyDown(KeyCode.M))
        {
            if (trajIntercepts != null)
            {
                trajIntercepts.ComputeAndMarkIntercepts(1f, 2f, 1f);
            }
        }
        else
        {
            // check for rotations
            Quaternion rotation = Quaternion.identity;
            if (Input.GetKey(KeyCode.A))
            {
                rotation        = Quaternion.AngleAxis(spinRate, Vector3.forward);
                rotationChanged = true;
            }
            else if (Input.GetKey(KeyCode.D))
            {
                rotation        = Quaternion.AngleAxis(-spinRate, Vector3.forward);
                rotationChanged = true;
            }
            else if (Input.GetKey(KeyCode.W))
            {
                rotation        = Quaternion.AngleAxis(spinRate, Vector3.right);
                rotationChanged = true;
            }
            else if (Input.GetKey(KeyCode.S))
            {
                rotation        = Quaternion.AngleAxis(-spinRate, Vector3.right);
                rotationChanged = true;
            }

            if (rotationChanged)
            {
                transform.rotation *= rotation;
            }
        }
        // When paused check for course correction
        if (!running)
        {
            bool thrustChanged = false;
            if (Input.GetKeyDown(KeyCode.Minus))
            {
                thrustSize -= thrustPerKeypress;
                if (thrustSize < 0)
                {
                    thrustSize = 0f;
                }
                thrustChanged = true;
            }
            else if (Input.GetKeyDown(KeyCode.Equals))
            {
                thrustSize   += thrustPerKeypress;
                thrustChanged = true;
            }
            if (thrustChanged || (rotationChanged && (thrustSize > 0)))
            {
                SetThrustCone(thrustSize);
                // To update the orbit prediction, need set the velocity
                thrust = transform.rotation * (thrustSize * axisN);
                // force back to initial velocity to add the current thrust
                GravityEngine.instance.UpdatePositionAndVelocity(nbody, shipPosition, shipVelocity);
                GravityEngine.instance.ApplyImpulse(nbody, thrust);
            }
        }
    }