![]() |
Listing 1. The movement algorithm in pseudocode.Back to article
Top of movement state loop: { If we're in IncrementWaypoint state: Increment our waypoint. If we're on a patrol Grab the next waypoint as defined by the patrol direction. Set state to WaitingForPath. Else If we're out of waypoints Set state to ReachedGoal. Else Set state to WaitingForPath. If we're in ReachedGoal state: Make the appropriate notifications (if any). We're done. Stop the walking animation. Exit function. If we're in WaitingForPath state: Find a path and save it. If we could not find one We've failed. Exit function. Calculate the direction we need to head in to get to our desired waypoint. Modify that direction by any limitations such as turn radius. Using that new direction, calculate where we'll end up after this move. If that new position causes a collision Set state to WaitingForPath. Jump back to the top of the loop. Using the current and future position: If we're closer to the waypoint before moving Set state to IncrementWaypoint Go back to top of loop. If we're going to jump over the waypoint during this move Set state to IncrementWaypoint. Break out of loop. } Set the accelerations accordingly. Do the actual move. Set or update any animation hooks that we might have. Update our predicted positionsBack to article