Here’s the listing:
Vector2D SteeringBehaviors::Interpose(const Vehicle* AgentA,
const Vehicle* AgentB)
//first we need to figure out where the two agents are going to be at
//time T in the future. This is approximated by determining the time
//taken to reach the midway point at the current time at max speed.
Vector2D MidPoint = (AgentA->Pos() + AgentB->Pos()) / 2.0;
double TimeToReachMidPoint = Vec2DDistance(m_pVehicle->Pos(), MidPoint) /
//now we have T, we assume that agent A and agent B will continue on a
//straight trajectory and extrapolate to get their future positions
Vector2D APos = AgentA->Pos() + AgentA->Velocity() * TimeToReachMidPoint;
Vector2D BPos = AgentB->Pos() + AgentB->Velocity() * TimeToReachMidPoint;
//calculate the midpoint of these predicted positions
MidPoint = (APos + BPos) / 2.0;
//then steer to arrive at it
return Arrive(MidPoint, fast);
Note that arrive is called with fast deceleration, allowing the vehicle to
reach the target position as quickly as possible.
The demo for this behavior shows a red vehicle attempting to interpose
itself between two blue wandering vehicles.
Hide attempts to position a vehicle so that an obstacle is always between
itself and the agent — the hunter — it’s trying to hide from. You can use
this behavior not only for situations where you require an NPC to hide
from the player — like find cover when fired at — but also in situations
where you would like an NPC to sneak up on a player. For example, you
can create an NPC capable of stalking a player through a gloomy forest,
darting from tree to tree. Creepy!
The method I prefer to effect this behavior is as follows:
Step One. For each of the obstacles, a hiding spot is determined. See Fig
How to Create Autonomously Moving Game Agents | 107
The Steering Behaviors