Пример #1
0
        // ----------------------------------------------------------------------------
        // tries to maintain a given speed, returns a maxForce-clipped steering
        // force along the forward/backward axis



        public Vector3 steerForTargetSpeed(float targetSpeed)
        {
            float mf         = maxForce();
            float speedError = targetSpeed - speed();

            return(forward() * OpenSteerUtility.clip(speedError, -mf, +mf));
        }
        // ----------------------------------------------------------------------------
        // apply a given steering force to our momentum,
        // adjusting our orientation to maintain velocity-alignment.


        public void applySteeringForce(Vector3 force, float elapsedTime)
        {
            Vector3 adjustedForce = adjustRawSteeringForce(force); //, elapsedTime);

            // enforce limit on magnitude of steering force
            //Vector3 clippedForce = adjustedForce.truncateLength (maxForce ());
            //Vector3 clippedForce = adjustedForce.truncateLength(maxForce());

            Vector3 clippedForce = truncateLength(adjustedForce, maxForce());

            // compute acceleration and velocity
            Vector3 newAcceleration = (clippedForce / mass());
            Vector3 newVelocity     = velocity();

            // damp out abrupt changes and oscillations in steering acceleration
            // (rate is proportional to time step, then clipped into useful range)
            if (elapsedTime > 0)
            {
                float smoothRate = OpenSteerUtility.clip(9 * elapsedTime, 0.15f, 0.4f);
                _smoothedAcceleration = OpenSteerUtility.blendIntoAccumulator(smoothRate,
                                                                              newAcceleration,
                                                                              _smoothedAcceleration);
            }

            // Euler integrate (per frame) acceleration into velocity
            newVelocity += _smoothedAcceleration * elapsedTime;

            // enforce speed limit

            //newVelocity = newVelocity.truncateLength (maxSpeed ());
            newVelocity = truncateLength(newVelocity, maxSpeed());


            // update Speed
            setSpeed(newVelocity.Length);


            // Euler integrate (per frame) velocity into position
            setPosition(Position + (newVelocity * elapsedTime));

            // regenerate local space (by default: align vehicle's forward axis with
            // new velocity, but this behavior may be overridden by derived classes.)
            regenerateLocalSpace(newVelocity); //, elapsedTime);

            // maintain path curvature information
            measurePathCurvature(elapsedTime);

            // running average of recent positions
            _smoothedPosition = OpenSteerUtility.blendIntoAccumulator(elapsedTime * 0.06f, // QQQ
                                                                      Position,
                                                                      _smoothedPosition);
        }