Steering Behaviors: Leader Following

Posted by: rocketman on October 29, 2010

The next post steering behaviors series is unaligned collision avoidance. We will be using the Vector2D.as and Vehicle.as classes for this. You will need these classes for the this post. This post is based on Craig Reynold’s article Steering Behaviors For Autonomous Characters.

Leader Following Click to watch

Leader Following is a steering behavior where vehicles follow one leader. Once a leader is chosen, all the other vehicles follow the leader. The followers want to be near the leader but not too close that the leader is covered in vehicles and without getting in his way. Also, the followers want to avoid running into each other.

Implementing Leader Following is simple using previous behaviors. The followers use the arrival behavior to follow the leader. The target of arrival is a point slightly behind the leader. This makes it so that the followers get near the leader, but not on top of him. Also because arrival causes the vehicles to slow down as they approach the target, the followers will not run the leader over.

The vehicles also want to avoid getting in the leaders path. To do this, each vehicle checks if it is within a area around the leader, the rectangle in the picture. If it is within the area, the vehicle moves laterally away from the leader.

Finally the vehicles want to avoid running into each other. This is done using separation from the flocking behavior.

Here is how Leader Following is done in code:

public function followLeader(leader:Vehicle, vehicles:Array):void {
  //each follower is given an array of all the other vehicles and the leader
  var leaderPos:Vector2D = leader.position.cloneVector();//get the leaders position
  //clone it so you can modify it
  if(leader.inSight(this)){//if the leader can see you
    var dist:Number = leaderPos.distance(position);//check the distance
    if(dist < 50) {//if you are within 50px
      seek(leaderPos.add(leader.velocity.perpendicular));//move away from where the leader is going
      return;//stop following
    }
  }
  //if you are not in the leaders way
  leaderPos.subtract(leader.velocity.cloneVector().normalize().multiply(10));//get a point behind the leader
  arrive(leaderPos);//arrive at it
  //separation for all other vehicles
  for(var i:int = 0; i < vehicles.length; i++) {
    var vehicle:Vehicle = vehicles[i] as Vehicle;
    if(tooClose(vehicle)) {
      flee(vehicle.position);
    }
  }
}

Click here to see Leader Following, the leader is yellow.

2DMathSteering BehaviorVector

Currently unrated


Comments

  • Patryk 1 year, 9 months ago

    Please report the picture and the .swf file. Thanks for the code - it's awesome :)

    Link / Reply

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, 8 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, 11 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, 11 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, 11 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, 11 months ago
RSS / Atom