I am trying to implement steering behaviours in my XNA game and mostly there but could use a hand with some little things. The behaviours work perfectly well, my issues lie in the timing and magnitudes of the updates.
(I am using SharpSteer (almost entirely at this point) it can be found here:
http://sharpsteer.codeplex.com/SourceControl/changeset/view/18102)
The Update() method is where I am struggling. Currently I am using this method (straight from SharpSteer)
// 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 = Vector3Helpers.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 = Utilities.Clip(9 * elapsedTime, 0.15f, 0.4f);
Utilities.BlendIntoAccumulator(smoothRate, newAcceleration, ref acceleration);
}
// Euler integrate (per frame) acceleration into velocity
newVelocity += acceleration * elapsedTime;
// enforce speed limit
newVelocity = Vector3Helpers.TruncateLength(newVelocity, MaxSpeed);
// update Speed
Speed = (newVelocity.Length());
// Euler integrate (per frame) velocity into position
Position = (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
Utilities.BlendIntoAccumulator(elapsedTime * 0.06f, // QQQ
Position,
ref smoothedPosition);
}
This works, but the changes from frame to frame are huge due to the elapsed time multiplier (I think SharpSteer uses it’s own game clock and not gameTime, I am using gameTime)
I am looking for a way to slow this down a lot, and to use speed values in a greater range than 1.0f.
Currently I am using a speed of 1.0f and a maxForce of 1.0f. I have noticed that Velocity is always (0,0,0) and elapsedTime is 16 (this is called 60 times per second)
smoothRate is always 0.4f, also because elapsedTime is too large.
If anyone could help be balance the speed of the vehicle somewhat I’d appreciate it.
increasing the mass will dampen (slowdown) acceleration for the same amount of force coming into the method. Are you sure you have the appropriate mass value set?
Also, is your velocity representing units per second? if so, then an elapsed time of 16 milliseconds should be 0.016;