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.

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 &lt; 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 &lt; vehicles.length; i++) {
var vehicle:Vehicle = vehicles[i] as Vehicle;
if(tooClose(vehicle)) {
flee(vehicle.position);
}
}
}

New Comment

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

4 years, 8 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