Jump to content
  • Advertisement
Sign in to follow this  
dabo

Arrive steering behavior

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

If you intended to correct an error in the post then please contact us.

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.



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.

Share this post


Link to post
Share on other sites
Advertisement
double speed = distance / ((double)deceleration * decelerationTweaker);
speed = Math.Min(speed, MaxSpeed);
Vector2 desiredVelocity = toTarget * speed / distance;
return desiredVelocity - Velocity;
[/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 this post


Link to post
Share on other sites
This is a copy paste from the simple soccer example:


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);
}

Share this post


Link to post
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 this post


Link to post
Share on other sites
Perhaps you can make a complete small program around the code you posted so we can run it on our own?


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

Share this post


Link to post
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 damped harmonic oscillator:

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 this post


Link to post
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 damped harmonic oscillator:

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.


Thanks I will look into that.

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Thank you for the tip.

I noticed that when I change this line:

Velocity = Velocity + acceleration * deltaTime;


into:

Velocity = Velocity + acceleration;


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 this post


Link to post
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.


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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!