Пример #1
0
        private void UpdatePhysicsLogic()
        {
            float throttleRate = 0;

            if (targetThrottle - throttle > 0)
            {
                throttleRate = accelRate * dt;
            }
            else if (targetThrottle - throttle < 0)
            {
                throttleRate = -reverseAccel * dt;
            }

            throttle = Mathf.Clamp(throttle + throttleRate, minThrottle, maxThrottle);

            if (targetThrottle == 0 && velocityMagnitude != 0)
            {
                targetBrkThrt = 1;
            }

            targetBrkThrt = Mathf.Clamp(targetBrkThrt - 1 * dt, 0, 1);
            brake         = maxBrakeTorque * targetBrkThrt;

            if (GoingBackwards && reversing != 2)
            {
                brake *= -1;
            }

            if (onGround)
            {
                motor = maxMotorTorque * throttle;
                if (currentPath.ActualPathExists())
                {
                    targetThrottle = ((!GoingBackwards && reversing != 0) || (GoingBackwards && reversing != 2)) ? minThrottle : maxThrottle;

                    var dirProjected = Vector3.ProjectOnPlane(100 * currentPath.NextActualWaypoint - 100 * mainRigidBody.worldCenterOfMass, mainRigidBody.transform.up);
                    dirProjected.Normalize();

                    Vector3 dir = dirProjected;

                    var   ff            = (GoingBackwards) ? -forward : forward;
                    float angleError    = Mathf.Clamp(Vector3.SignedAngle(ff, dir, mainRigidBody.transform.up), -maxSteeringAngle, maxSteeringAngle);
                    float velocityError = maxVelocity - velocityMagnitude;

                    float mult = 1;
                    float velocityCorrectionForMotor = velocityController.GetOutput(velocityError, dt, mult, mult, mult);

                    steering = angleError * Mathf.Sign(throttle);

                    if (velocityError / maxVelocity < 0.1)
                    {
                        motor *= Mathf.Min(velocityCorrectionForMotor, 0.95f);
                    }
                }
                else
                {
                    targetThrottle = 0;
                }
            }
            else
            {
                targetThrottle = 0;
            }
        }
        private void UpdatePathtrackingPhysics()
        {
            float throttleRate = 0;

            if (targetThrottle - throttle > 0)
            {
                throttleRate = accelRate * dt;
            }
            else if (targetThrottle - throttle < 0)
            {
                throttleRate = -brakeRate * dt;
            }

            throttle = Mathf.Clamp(throttle + throttleRate, minThrottle - throttleRate, targetThrottle);

            /*
             * if (throttle < 0)
             *  Debug.Log("brakkkkeee : " + throttle);
             */

            if (onGround)
            {
                mainRigidBody.AddRelativeForce(-1 * mainRigidBody.transform.up, ForceMode.Acceleration);

                if (!pausedCourse && currentPath.ActualPathExists())
                {
                    targetThrottle = (!reversing) ? maxThrottle : minThrottle;

                    var dirProjected = Vector3.ProjectOnPlane(100 * currentPath.NextActualWaypoint - 100 * mainRigidBody.worldCenterOfMass, mainRigidBody.transform.up);
                    dirProjected.Normalize();

                    Vector3 dir = (currentPath.orientingAtLastWaypoint) ? currentPath.DesiredOrientationAtLastWaypoint : dirProjected;

                    Vector3 forward = (!goingBackwards) ? mainRigidBody.transform.forward : -mainRigidBody.transform.forward;

                    //float angleError = Mathf.DeltaAngle(transform.eulerAngles.y, targetAngle);
                    float angleError = Mathf.Clamp(-angleErrorClampValue, Vector3.SignedAngle(forward, dir, mainRigidBody.transform.up), angleErrorClampValue);
                    var   tratio     = (throttle > 0) ? throttle / maxThrottle : -throttle / minThrottle;
                    var   mult       = anglePIDKmultFromThrottleRatio.Evaluate(tratio);
                    float torqueCorrectionForAngle = angleController.GetOutput(angleError, dt, mult, mult, mult);

                    float angularVelocityError = Mathf.Clamp(Vector3.Dot(angularVelocity, mainRigidBody.transform.up), -maxAngularVelocity / 2, maxAngularVelocity / 2);
                    float torqueCorrectionForAngularVelocity = angularVelocityController.GetOutput(angularVelocityError, dt, mult, mult, mult);

                    float velocityError = maxVelocity - velocityMagnitude;
                    float forceCorrectionForVelocity = velocityController.GetOutput(velocityError, dt, mult, mult, mult);

                    Vector3 torque = mainRigidBody.transform.up;

                    torque *= torqueFactor;
                    torque *= torqueCorrectionForAngle + torqueCorrectionForAngularVelocity;

                    var force = mainRigidBody.transform.forward;
                    force.y = 0;
                    force.Normalize();

                    force *= throttle;
                    if (velocityError / maxVelocity < 0.08)
                    {
                        force *= Mathf.Min(forceCorrectionForVelocity, 0.99f);
                    }

                    mainRigidBody.AddTorque(torque, ForceMode.Acceleration);

                    mainRigidBody.AddForce(force, ForceMode.Acceleration);
                }
                else
                {
                    targetThrottle = 0;
                }
                if (slipReductionFactor != 0)
                {
                    var slipReductionForce = -slipReductionFactor *Vector3.Project(velocity, mainRigidBody.transform.right) / dt;  // slip / traction reduction

                    mainRigidBody.AddForce(slipReductionForce, ForceMode.Acceleration);
                }
            }
            else
            {
                targetThrottle = 0;
            }
        }