Wall Following steering behavior

In this demonstration, the green vehicle uses wall following behavior to move parallel to and offset from the areas marked in gray. The vehicle's goal is to remain a given distance from the "wall" as it moves. When it goes off the window its position is wrapped around to the other side. The vehicle's velocity is indicated by a magenta vector, and its steering force is indicated by a blue vector.

To implement wall following we begin by predicting the vehicle's position a short time in the future based on its current velocity (black dot). This future position is projected to the nearest point on the "wall" (red dot). Moving out from this wall point along the wall normal (red line) by the desired offset distance produces a target point (red circle). Finally, seek behavior is used to steer toward the target point.

Communication between vehicle and wall is handled by a generic surface protocol: the vehicle passes a point to the surface and asks for the nearest point on the wall and the normal at that point. As a result, the steering behavior needs no knowledge of the surface's shape. For example, this same object is used unchanged in another demonstration.

From
Steering Behaviors for Autonomous Characters