Original Post
Hello everyone,
I am trying to implement the arrive steering behavior that can be found in the book "Programming Game AI by Example". My project is in C# so I had to rewrite the code that came with the book. Below is the relevant part of my steering behavior class.
And this is how one of my game objects uses the steering behavior class:
The problem is that I get an oscillating behavior around the target position, the object oscillates less and less and after awhile it stops at the target position. Can anyone see anything in the code I have posted that would cause this oscillating behavior? I fail to see what I have done wrong when I rewrote the code into C# from C++. The samples accompanying the book obviously work so I don't expect any errors in the original code.
I appreciate any help.
I am trying to implement the arrive steering behavior that can be found in the book "Programming Game AI by Example". My project is in C# so I had to rewrite the code that came with the book. Below is the relevant part of my steering behavior class.
public Vector2 SteeringForce { get; private set; }
public Vector2 Target { get; set; }
private enum Deceleration
{
Fast = 1,
Normal = 2,
Slow = 3
}
private Vector2 Arrive(Vector2 target, Deceleration deceleration)
{
Vector2 toTarget = target - Position;
double distance = toTarget.Length();
if (distance > 0)
{
//because Deceleration is enumerated as an int, this value is required
//to provide fine tweaking of the deceleration..
double decelerationTweaker = 0.3;
double speed = distance / ((double)deceleration * decelerationTweaker);
speed = Math.Min(speed, MaxSpeed);
Vector2 desiredVelocity = toTarget * speed / distance;
return desiredVelocity - Velocity;
}
return new Vector2();
}
private Vector2 SumForces()
{
Vector2 force = new Vector2();
if (Activated(BehaviorTypes.Arrive))
{
force += Arrive(Target, Deceleration.Fast);
if (!AccumulateForce(force))
return SteeringForce;
}
return SteeringForce;
}
private bool AccumulateForce(Vector2 forceToAdd)
{
double magnitudeRemaining = MaxForce - SteeringForce.Length();
if (magnitudeRemaining <= 0)
return false;
double magnitudeToAdd = forceToAdd.Length();
if (magnitudeToAdd > magnitudeRemaining)
magnitudeToAdd = magnitudeRemaining;
SteeringForce += Vector2.Normalize(forceToAdd) * magnitudeToAdd;
return true;
}
public Vector2 Calculate()
{
SteeringForce.Zero();
SteeringForce = SumForces();
SteeringForce.Truncate(MaxForce);
return SteeringForce;
}
And this is how one of my game objects uses the steering behavior class:
public override void Update(double deltaTime)
{
Vector2 steeringForce = SteeringBehaviors.Calculate();
Vector2 acceleration = steeringForce / Mass;
Velocity = Velocity + acceleration * deltaTime;
Velocity.Truncate(MaxSpeed);
Position = Position + Velocity * deltaTime;
}
The problem is that I get an oscillating behavior around the target position, the object oscillates less and less and after awhile it stops at the target position. Can anyone see anything in the code I have posted that would cause this oscillating behavior? I fail to see what I have done wrong when I rewrote the code into C# from C++. The samples accompanying the book obviously work so I don't expect any errors in the original code.
I appreciate any help.