Waypoint Navigation

This sample demonstrates basic navigation using waypoints.

Sample Overview

When programming the AI for your game, you often want your actors to follow a path that you supply. For example, you may want units in your RTS to follow orders, or perhaps you want to make a mouse navigate a maze.

This sample uses a tank to demonstrate two examples of this waypoint following behavior. In this sample, you can move the cursor around and add waypoints toward which the tank will move in the order that you add them.

Note
This sample uses code and concepts from the Chase and Evade and Fuzzy Logic samples, and assumes that the reader is familiar with the code and concepts explained in those samples.

Sample Controls

This sample uses the following keyboard and gamepad controls.

Action Keyboard Control Gamepad Control Windows Phone
Move the cursor. UP ARROW, DOWN ARROW, LEFT ARROW, RIGHT ARROW Left Thumbstick Drag the cursor
Place a waypoint. A A Release the cursor on the screen
Reset. X X Touch the "Clear" button
Change the waypoint following behavior. B B Touch the behavior button
Exit the game. ESC or ALT+F4 BACK BACK

How the Sample Works

The sample implements two types of behavior, LinearBehavior and SteeringBehavior, that determine how the tank moves from waypoint to waypoint.

LinearBehavior

LinearBehavior causes the tank to move directly and abruptly to its waypoint. It does this by setting the tank's Tank.MoveSpeed to its Tank.MaxMoveSpeed and setting its Tank.Direction to be exactly towards its current waypoint at all times. This makes the tank change direction instantly and always move in a straight line towards the waypoint.

SteeringBehavior

SteeringBehavior causes the tank to turn and to speed up or slow down smoothly as it moves toward the waypoint. It does this by using some geometry in SteeringBehavior.FindMaxMoveSpeed to determine its target velocity. Then, to turn smoothly, it uses SteeringBehavior.TurnToFace from the Chase and Evade sample.

Given an angular velocity w and a linear velocity v, the turning radius r of the tank is:

r = v/w

Anything inside a circle tangent to the tank's position on either side of it is inside the tank's turning radius and cannot be reached at its current velocity.

The solution is to estimate how fast the tank can move while still managing to reach its destination. We do this by finding the radius of the largest circle we can draw between the tank and its destination. Then, given this new radius, we use the following formula to determine a speed that guarantees that the tank can intercept its destination:

v = r * w

Extending the Sample

  • Add obstacles and a second behavior that causes the tank to move around the obstacles as it moves from waypoint to waypoint.
  • Both tank behaviors have access to the list of waypoints, but both types of movement make use of the first remaining waypoint on the list. Create a new behavior that uses the built-in Curve class to fit all the waypoints onto a smooth curve that the tank can follow.