Steering Behaviors: Pursuit and Evade

Posted by: rocketman on June 14, 2010

Up next is pursuit. Again, we will be using the Vehicle.as class. It's the next on in the steering behaviors series. It is based on Craig Reynolds' article, Steering Behaviors For Autonomous Characters.

Pursuit is the most complicated behavior we've done so far. To pursue, there needs to be another moving target which you want to pursue. To pursue, you look at the current vehicle, use its current velocity, and predict where it will be in T. Then you simply seek the predicted point. Now pursuit assumes that the other vehicle is taking a straight path. This is often not the case so it will pursue the wrong point. But it will only go after that point for 1/30 of a second(assuming the frame rate is 30FPS). Then it will correct itself.

The tricky part is calculating T, the amount of time to look ahead. You could simply use a constant (which would make a improved seek, but we can do better. Seek is T=0) , or you could get fancy with it. T should get larger as the target goes away and T should get smaller as you approach the target. T = distance/maxSpeed.

It works like this:

1. Take both vehicles, find the distance between them.

2. Take the distance and divide by the target's maxSpeed.

3. Take the target's velocity, multiply it by T and add it to the target's position.

4. Seek that new position

5. Update the vehicle

In code:

public function pursue(target:Vehicle):void {
var distance:Number = target.position.distance(position);
var T:Number = distance / target._maxSpeed;
var targetPosition:Vector2D = target.position.cloneVector().add(target.velocity.cloneVector().multiply(T));
seek(targetPosition);
}

Click here to see it in action! In the example, on vehicle is pursuing the other which is wandering. You can see how good pursuit is at catching the other vehicle.

Pursuit BehaviorEvasion Behavior. Click to watch.

Evasion is the opposite of pursuit. You take the target vehicle, predict its position in the future then flee that point.

public function evade(target:Vehicle):void {
var distance:Number = target.position.distance(position);
var T:Number = distance / target._maxSpeed;
var targetPosition:Vector2D = target.position.cloneVector().add(target.velocity.cloneVector().multiply(T));
flee(targetPosition);
}

In the example, the one vehicle is pursuing the evader. Eventually, the pursuer wins, but it takes much more time than in the first example.

Steering Behavior2DMathVector

Currently unrated


Comments

  • There are currently no comments

New Comment

required
required (not published)
optional
  • Back in July, we looked at how to use Ray Casting for collision detection. We also learned how to use the Separation of Axis Theorem. I recently had a request for a way to use the Shapes we created here with the Ray Casting method.¬†First, lets do a quick review of the shape classes. read more
    3 years, 7 months ago
  • Today we will finally be detecting a collision with SAT. We know how SAT works, we've built classes to work with SAT, now we can use all of this to detect a real, live collision! read more
    3 years, 9 months ago
  • A few months ago, I posted on the separation of axis theorem. You can learn all about SAT and how it works here. What that post failed to do was use the SAT. We will explore using SAT for collision detection in this post. read more
    3 years, 10 months ago
  • Some of you have asked for the entire steering behavior source as well as the A* source. Here is the zip with all the classes. In there is the vehicle... read more
    3 years, 10 months ago
  • In the last three posts, we explored the how A* works, then we put A-star into code, then we looked at different heuristics for A*. Now we will combine A*... read more
    3 years, 10 months ago
RSS / Atom