# Arrive steering behavior

This topic is 1877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

[CODE]

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;
}
{
double magnitudeRemaining = MaxForce - SteeringForce.Length();
if (magnitudeRemaining <= 0)
return false;
return true;
}
public Vector2 Calculate()
{
SteeringForce.Zero();
SteeringForce = SumForces();
SteeringForce.Truncate(MaxForce);
return SteeringForce;
}
[/CODE]

And this is how one of my game objects uses the steering behavior class:
[CODE]
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;
}
[/CODE]

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.

##### Share on other sites
[quote][code] double speed = distance / ((double)deceleration * decelerationTweaker);
speed = Math.Min(speed, MaxSpeed);
Vector2 desiredVelocity = toTarget * speed / distance;
return desiredVelocity - Velocity;
[/code][/quote]

This is fishy. Notice how speed is proportional to distance, but then it gets divided by distance. This means your desiredVelocity is simply toTarget / some_constant. The resulting dynamics are those of an undamped pendulum, so of course you get oscillatory behavior. I don't have the book in front of me to compare the code, but that can't be what they are doing. Edited by Álvaro

##### Share on other sites
This is a copy paste from the simple soccer example:

[CODE]
Vector2D SteeringBehavior::Arrive(Vector2D TargetPos, Deceleration deceleration)
{
Vector2D ToTarget = TargetPos - m_pVehicle->Pos();
//calculate the distance to the target
double dist = ToTarget.Length();
if (dist > 0)
{
//because Deceleration is enumerated as an int, this value is required
//to provide fine tweaking of the deceleration..
const double DecelerationTweaker = 0.3;
//calculate the speed required to reach the target given the desired
//deceleration
double speed = dist / ((double)deceleration * DecelerationTweaker);
//make sure the velocity does not exceed the max
speed = min(speed, m_pVehicle->MaxSpeed());
//from here proceed just like Seek except we don't need to normalize
//the ToTarget vector because we have already gone to the trouble
//of calculating its length: dist.
Vector2D DesiredVelocity = ToTarget * speed / dist;
return (DesiredVelocity - m_pVehicle->Velocity());
}
return Vector2D(0,0);
}
[/CODE]

##### Share on other sites
Oh, actually I misread the code. I thought you were returning the result of that computation directly as force, but you are actually subtracting your current velocity for it, so I guess that's OK.

I am not sure what the problem is. Perhaps you can make a complete small program around the code you posted so we can run it on our own?

##### Share on other sites
[quote name='Álvaro' timestamp='1353456575' post='5002803']Perhaps you can make a complete small program around the code you posted so we can run it on our own?
[/quote]

Ok I will do that when I get home from work.

##### Share on other sites
I guess it might be a matter of adjusting parameters correctly. The parametrization used in that code is a bit strange to me, but you essentially have a differential equation that looks like a [url="http://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator"]damped harmonic oscillator[/url]:

force = -k * (position - target_position) - c * velocity

You'll then compute acceleration as force/mass. The damping ratio is defined as c/(2*sqrt(m*k)). If your damping ratio is under 1, you'll have oscillations. You should aim for a damping ratio of 1 or just a little above. As usual, experimentation is the only way to tell what feels right in your situation.

Of course you can cap the magnitude of the force to some maximum value. Edited by Álvaro

##### Share on other sites
[quote name='Álvaro' timestamp='1353504674' post='5002910']
I guess it might be a matter of adjusting parameters correctly. The parametrization used in that code is a bit strange to me, but you essentially have a differential equation that looks like a [url="http://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator"]damped harmonic oscillator[/url]:

force = -k * (position - target_position) - c * velocity

You'll then compute acceleration as force/mass. The damping ratio is defined as c/(2*sqrt(m*k)). If your damping ratio is under 1, you'll have oscillations. You should aim for a damping ratio of 1 or just a little above. As usual, experimentation is the only way to tell what feels right in your situation.

Of course you can cap the magnitude of the force to some maximum value.
[/quote]

Thanks I will look into that.

I have created a minimal program showing the problem, now you can test it yourself. [url="http://www.dabostudios.net/Arrive.zip"]http://www.dabostudios.net/Arrive.zip[/url]

##### Share on other sites
hello.
Try playing with the DecelerationTweaker Im using 1.3
I only get that problem when the speed is to high.

##### Share on other sites
Thank you for the tip.

I noticed that when I change this line:
[CODE]
Velocity = Velocity + acceleration * deltaTime;
[/CODE]

into:
[CODE]
Velocity = Velocity + acceleration;
[/CODE]

It appears to work like I expect, it also fixes the seek steering behavior. Can anyone explain this? I thought deltaTime had to be included when calculating both velocity and position. Maybe there is still a problem hidden somewhere.

EDIT: I should add, at higher speeds I need to increase the max force as well. Edited by dabo

##### Share on other sites
[quote name='ankhd' timestamp='1353656302' post='5003402']
hello.
Try playing with the DecelerationTweaker Im using 1.3
I only get that problem when the speed is to high.
[/quote]

Did you change something else too? Because if I only change the deceleration tweaker to 1.3 I still get the same problem at default max speed 1.6 for example.

##### Share on other sites
[quote name='dabo' timestamp='1353775438' post='5003771']
Thank you for the tip.

I noticed that when I change this line:
[CODE]
Velocity = Velocity + acceleration * deltaTime;
[/CODE]

into:
[CODE]
Velocity = Velocity + acceleration;
[/CODE]

It appears to work like I expect, it also fixes the seek steering behavior. Can anyone explain this? I thought deltaTime had to be included when calculating both velocity and position. Maybe there is still a problem hidden somewhere.
[/quote]

You should definitely keep the multiplication by deltaTime in place. If removing it gives you the behavior you like, you can also get it by dividing the force by deltaTime.

It looks to me like the setup in the code you are using is strange and complicated. I would consider much simpler code, like this (in C++):

[code]Vector2D force_to_arrive(Point2D position, Vector2D velocity, Point2D target_position) {
const double c = 4.0;
const double k = 4.0;

Vector2D position_difference = target_position - position;
return k * position_difference - c * velocity;
}[/code] Edited by Álvaro

##### Share on other sites

Yes I thought keeping deltaTime in the calculation was correct. I am just confused as to why the book author's code work but not mine, I don't see any difference and I have tried using the same default values for mass, max force, max speed.

##### Share on other sites
I found a C# implementation of the same example from the book and it had no parenthesis around deceleration * decelerationTweaker like this:

[CODE]
double speed = distance / (double)deceleration * decelerationTweaker;
[/CODE]

When I changed this in my code it appeared to work even with deltaTime:

[CODE]
Velocity = Velocity + acceleration * deltaTime;
[/CODE]

I am really confused why I need to remove the parenthesis when all other implementations I have seen (except the one I mentioned above) uses them. I have also tested implementations not from the book and they also result in this oscillating behavior when I use it in my code.

##### Share on other sites
Did you change something else too?.

YES.

I also did what you did like this here.
Velocity = Velocity + acceleration;
note I jast added it back then and tryed it. And it has the same problem.

But this is my code here I thik its the way to go.

SteeringForce = SteeringBehaviors->Calculate();

//Acceleration = Force/Mass
D3DXVECTOR3 acceleration = SteeringForce / ComponentMotion->m_dMass;

//update velocity
ComponentMotion->m_vVelocity += acceleration;// * timedelta; REMOVED IT HERE
//make sure vehicle does not exceed maximum velocity

ComponentMotion->m_vVelocity = Truncate(ComponentMotion->m_vVelocity, ComponentMotion->m_dMaxSpeed);
//update the position
ComponentMotion->m_vPos += ComponentMotion->m_vVelocity * timedelta;