Ejemplo n.º 1
0
    protected void FireThrusters(Vector3 vOrientationProp)
    {
        //for each axis, applies torque based on the torque available to the axis in the direction indicated by the activation value.
        //-1 means we are applying torque to effect a negative rotation.  +1 does just the opposite.  0 means no torque is needed.
        Vector3 vThrustProp = vOrientationProp;
        Vector3 vXTorque    = Vector3.zero;
        Vector3 vYTorque    = Vector3.zero;
        Vector3 vZTorque    = Vector3.zero;

        if (!InputManager.Instance.NoviceMode)
        {
            vXTorque = tActivation.x * MTransform.TransformDirection(Vector3.right) * torques.x * Mathf.Abs(vThrustProp.x) * Time.fixedDeltaTime;
            vYTorque = tActivation.y * MTransform.TransformDirection(Vector3.up) * torques.y * Mathf.Abs(vThrustProp.y) * Time.fixedDeltaTime;
            vZTorque = tActivation.z * MTransform.TransformDirection(Vector3.forward) * torques.z * Mathf.Abs(vThrustProp.z) * Time.fixedDeltaTime;
        }
        else
        {
            Vector3 vLocalRight = MTransform.TransformDirection(Vector3.right);
            vLocalRight.y = 0;

            vXTorque = tActivation.x * vLocalRight.normalized * torques.x * Mathf.Abs(vThrustProp.x) * Time.fixedDeltaTime;
            vYTorque = tActivation.y * (Vector3.up) * torques.y * Mathf.Abs(vThrustProp.y) * Time.fixedDeltaTime;
        }

        if (!MLandingAbility.Landed)
        {
            Vector3 localVelocity = MTransform.InverseTransformDirection(m_rigidbody.velocity);
            float   ForwardSpeed  = Mathf.Max(0, localVelocity.z);

            Vector3 vTorque = Vector3.zero;

            if (InputManager.Instance.NoviceMode)
            {
                Quaternion localRotation = MTransform.localRotation;
                Quaternion axisRotation  = Quaternion.AngleAxis(localRotation.eulerAngles[0], rotateAround);

                float angleFromMin = Quaternion.Angle(axisRotation, minQuaternion);
                float angleFromMax = Quaternion.Angle(axisRotation, maxQuaternion);

                float fDotUp           = Vector3.Dot(MTransform.forward, Vector3.up);
                float fDotDown         = Vector3.Dot(MTransform.forward, -Vector3.up);
                bool  bDiffUp          = (1f - fDotUp) < 0.1f;
                bool  bDiffDown        = (1f - fDotDown) < 0.1f;
                bool  bDiff            = bDiffUp || bDiffDown;
                bool  bDownDirMovement = bDiffUp && !bDiffDown && tActivation.x > 0;
                bool  bUpDirMovement   = bDiffDown && !bDiffUp && tActivation.x < 0;

                if (!bDiff || bDownDirMovement || bUpDirMovement)
                {
                    if (tActivation.x != 0)
                    {
                        vTorque += vXTorque;
                    }
                }
                else
                {
                    Vector3 vAngular = m_rigidbody.angularVelocity;
                    vAngular.x = 0f;
                    vAngular.z = 0f;
                    m_rigidbody.angularVelocity = vAngular;
                }

                if (tActivation.y != 0)
                {
                    vTorque += vYTorque;
                }
            }
            else
            {
                if (tActivation.x != 0)
                {
                    vTorque += vXTorque;
                }
                if (tActivation.y != 0)
                {
                    vTorque += vYTorque;
                }
                if (tActivation.z != 0)
                {
                    vTorque += vZTorque;
                }
            }

            if (AllowTorquing && !InputManager.Instance.NoviceMode)
            {
                m_rigidbody.AddTorque(vTorque);
            }
            else if (AllowTorquing && InputManager.Instance.NoviceMode)
            {
                m_rigidbody.AddTorque(vTorque);
            }
        }

        if (InputManager.Instance.IsMovement() && !m_playerShip.m_airBrakePress.IsActive)
        {
            SpeedStat.Accelerate();

            if (MTransform.forward.y < 0f)
            {
                Vector3 vDownForce = Physics.gravity.y * GameSimulation.Instance.GravityMultiplier * MTransform.forward.y * MTransform.forward * GameSimulation.Instance.GravityMultiplier;
                m_rigidbody.AddForce(vDownForce, ForceMode.Force);
            }
        }
        else
        {
            SpeedStat.Decelerate();
        }

        if (!MLandingAbility.Landed)
        {
            SpeedStat.AddForce(vThrustProp.x, vThrustProp.y);
        }

        bool bDashCheck = m_playerShip.MDashAbility == null ||
                          (m_playerShip.MDashAbility != null && m_playerShip.MDashAbility.Cooldown.IsMin()) ||
                          (m_playerShip.MSpeedGateEffect.ForcesActive());

        if (m_bAeroSteering)
        {
            if ((Rigidbody.velocity.magnitude > 0 && bDashCheck) || DisableAeroDynamic)
            {
                AeroDynamicEffect.ModifyCurrent(Time.fixedDeltaTime * m_fAeroGainMod);
                // compare the direction we're pointing with the direction we're moving:
                m_AeroFactor = Vector3.Dot(MTransform.forward, Rigidbody.velocity.normalized);
                // multipled by itself results in a desirable rolloff curve of the effect
                m_AeroFactor *= m_AeroFactor;
                // Finally we calculate a new velocity by bending the current velocity direction towards
                // the the direction the plane is facing, by an amount based on this aeroFactor
                Vector3 newVelocity = Vector3.Lerp(Rigidbody.velocity, MTransform.forward * Rigidbody.velocity.magnitude,
                                                   m_AeroFactor * Rigidbody.velocity.magnitude * AeroDynamicEffect.Current * Time.fixedDeltaTime);

                Rigidbody.velocity = newVelocity;
            }
            else
            {
                AeroDynamicEffect.SetToMin();
            }
        }
    }
Ejemplo n.º 2
0
    public void ExperimentalOrientationChange(Vector3 vDir, float fMaxDegrees)
    {
        Quaternion desiredRotation = Quaternion.LookRotation(vDir);

        desiredRotation = Quaternion.RotateTowards(Transform.rotation, desiredRotation, fMaxDegrees);

        float      kp  = (6f * m_fFrequency) * (6f * m_fFrequency) * 0.25f;
        float      kd  = 4.5f * m_fFrequency * m_fDamping;
        float      dt  = Time.fixedDeltaTime;
        float      g   = 1 / (1 + kd * dt + kp * dt * dt);
        float      ksg = kp * g;
        float      kdg = (kd + kp * dt) * g;
        Vector3    x;
        float      xMag;
        Quaternion q = desiredRotation * Quaternion.Inverse(MTransform.rotation);

        q.ToAngleAxis(out xMag, out x);
        x.Normalize();
        x *= Mathf.Deg2Rad;
        Vector3    pidv             = kp * x * xMag - kd * Rigidbody.angularVelocity;
        Quaternion rotInertia2World = Rigidbody.inertiaTensorRotation * MTransform.rotation;

        pidv = Quaternion.Inverse(rotInertia2World) * pidv;
        pidv.Scale(Rigidbody.inertiaTensor);
        pidv = rotInertia2World * pidv;

        bool bNaNCheck = float.IsNaN(pidv.x) || float.IsNaN(pidv.y) || float.IsNaN(pidv.z);

        if (!bNaNCheck)
        {
            if (pidv.magnitude > fMaxDegrees)
            {
                pidv = pidv.normalized * fMaxDegrees;
            }

            Rigidbody.AddTorque(pidv);

            if (pidv.sqrMagnitude <= 0.1f)
            {
                m_bReOrientLock = true;
            }
        }

        if (InputManager.Instance.IsMovement())
        {
            SpeedStat.Accelerate();

            if (MTransform.forward.y < 0f)
            {
                Vector3 vDownForce = Physics.gravity.y * GameSimulation.Instance.GravityMultiplier * MTransform.forward.y * MTransform.forward * GameSimulation.Instance.GravityMultiplier;
                m_rigidbody.AddForce(vDownForce, ForceMode.Force);
            }
        }
        else
        {
            SpeedStat.Decelerate();
        }

        if (!MLandingAbility.Landed)
        {
            SpeedStat.AddForce(0f, 0f);
        }

        bool bDashCheck = m_playerShip.MDashAbility == null ||
                          (m_playerShip.MDashAbility != null && m_playerShip.MDashAbility.Cooldown.IsMin()) ||
                          (m_playerShip.MSpeedGateEffect.ForcesActive());

        if ((Rigidbody.velocity.magnitude > 0 && bDashCheck) || DisableAeroDynamic)
        {
            AeroDynamicEffect.ModifyCurrent(Time.fixedDeltaTime * m_fAeroGainMod);
            // compare the direction we're pointing with the direction we're moving:
            m_AeroFactor = Vector3.Dot(MTransform.forward, Rigidbody.velocity.normalized);
            // multipled by itself results in a desirable rolloff curve of the effect
            m_AeroFactor *= m_AeroFactor;
            // Finally we calculate a new velocity by bending the current velocity direction towards
            // the the direction the plane is facing, by an amount based on this aeroFactor
            Vector3 newVelocity = Vector3.Lerp(Rigidbody.velocity, MTransform.forward * Rigidbody.velocity.magnitude,
                                               m_AeroFactor * Rigidbody.velocity.magnitude * AeroDynamicEffect.Current * Time.fixedDeltaTime);

            Rigidbody.velocity = newVelocity;
        }
        else
        {
            AeroDynamicEffect.SetToMin();
        }
    }