// ---------------------------------------------------------------------------- // 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.magnitude); // 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); }