Basic Pathfinding

advertisement
Pathfinding
Basic Methods
Pathfinding
 Different types of pathfinding problems exist  No one
solution appropriate to every problem!
 Qs:
 Is the destination moving or stationary?
 Are there obstacles?
 What is the terrain like?
 Is the shortest solution always the best solution?
 Is it required to reach a specific destination or just a partial
route will do?
 What map representation is used?
Pathfinding
 You have heard of the A* Algorithm, the most popular &
famous pathfinding algorithm
 But we will first look at some basic pathfinding methods (that
are not as complex as A*and more suitable than A* in various
situations)…
 A* will be covered in the next lesson
Basic Pathfinding
 The most simple, easiest and fastest solution to pathfinding is
to re-use the Chase movement…but “chase” a destination
position!
if(positionX > destinationX) positionX--;
else if(positionX < destinationX) positionX++;
if(positionY > destinationY) positionY--;
else if(positionY < destinationY) positionY++;
 Probably produces the most unnatural-looking path
Basic Pathfinding
 OK, the better approach would be to use the Line-of-Sight
Chase to get a more natural path (using line-drawing
algorithm on TBE and steering forces on CE)
 However, these methods are not suitable for certain
scenarios. Can you figure out?
Obstacles?
 Problems with obstacles
 Random Movement Obstacle Avoidance
Simple and effective method
Works well in environments with relative few obstacles
if Player In Line of Sight
Follow Path to Player
else
Move in Random Direction
Tracing Around Obstacles
 Another simple method – Tracing Around Obstacles
 When encounter obstacle, switch to “tracing” state
 Tracing  follows the edge of the obstacle to work way
around it
Tracing Around Obstacles
 Another simple method – Tracing Around Obstacles
 When encounter obstacle, switch to “tracing” state
 Tracing  follows the edge of the obstacle to work way
around it
 Basic Tracing Movement:
Tracing Around Obstacles
 We need to decide WHEN to STOP tracing!
 One easy way: Calculate a line from the point the tracing
starts to the desired destination
 Continue Tracing until that line is crossed, then revert back
to L-o-S pathfinding
 Improved Tracing:
Tracing Around Obstacles
 Incorporate line-of-sight with tracing method
 At each step of tracing state, utilize line-of-sight to determine if
a straight line-of-sight path can be followed to reach destination
immediately
 If a line-of-sight path is possible, switch back to line-of-sight
pathfinding state
 Tracing with Line-of-Sight:
Breadcrumb Pathfinding
 Able to make NPCs appear intelligently, because the player is
unknowingly creating the path for the NPC!
 Each time the player takes a step, he leaves an invisible
marker or “breadcrumb” in the game world
 Breadcrumb trail:
Breadcrumb Pathfinding
 When the NPC encounters a breadcrumb, it simply begins to
follow the trail until the end
 In a real game, the number of breadcrumbs dropped will
depend on the game and how smart you want the NPCs to
appear.
 The player never sees the breadcrumb trail!
Breadcrumb Pathfinding
 Implementation
 Begin by creating a trail row and column arrays and setting each
element value to -1
 Checks for the player’s direction key presses and records them
down  “dropping a breadcrumb”
 Since there is a max trail length, the oldest position will be
dropped so that a new position can be added
Following the breadcrumbs
 Example: Troll moves randomly (8 possible directions)
 Loop through the trail locations to determine if the troll has
moved into a breadcrumb location
 If a breadcrumb is found, set the troll to use the path
Following the breadcrumbs
 Due to possibility that the player’s path overlap itself or
adjacent to previous location in path
 Not Smart: NPC ends up taking exact footsteps of player
 Solution: Allow NPC to always look for adjacent tile
containing the most recent breadcrumb, skipping over
breadcrumbs
Path Following (in TBE)
 To confine a NPC to a certain terrain element such as road,
we can use terrain labeling
 Example: Labeling road terrain tiles as 2s and other out-of-
bounds terrain as 1s
Path Following (in TBE)
 We do NOT want to make it move randomly allow the road




tiles  Unnatural
From 8 possible directions to move, eliminate those that are
not part of the road. Then, decide on which of the remaining
directions to take
Set neighboring road tiles to a big number and those out-ofbounds to 0.
Tip: Keep the NPC moving in the same
general direction, turn only when have to
Tip: Assign a number to each direction
Path Following (in TBE)
 Weigh the directions so that priority will be given to
maintaining previous direction
 Example: Current direction: 1
 Traverse the direction array in search for the
most highly weighted direction, move to that
direction in the next step
Path Following (in TBE)
 How do we increase the robustness of this path following
movement (to look more natural and intelligent)?
Wall Tracing
 Does not calculate path from a starting to ending point
 Most useful in game environments with many rooms or
mazes
 Obstacle tracing (discussed earlier) can also be used to trace
walls
 Random movement in such environments
is commonly used to increase uncertainty
but NPCs often get stuck in small rooms
for long periods of time
Wall Tracing
 How to make the troll EXPLORE and move in a systematic
way around this environment?
Wall Tracing
 Simple solution  Left-handed approach
 If the NPC always move to the left, it will do a thorough job
exploring the environment
 Move LEFT WHENEVER POSSIBLE (Remember: Left of the
NPC, not the player!)
 Example: NPC facing player’s right
 Direction 2 is the NPC’s LEFT
 If that is blocked try STRAIGHT, then
try RIGHT
 If still blocked, choose to reverse
BACK
Wall Tracing
 Implementation: 4 IF-ELSE blocks to check for the directions
to take (8 if accommodating all 8 directions!)
 Should be able to traverse almost every room,
but not guaranteed to work in all geometries!
Waypoint Navigation
 Pathfinding  Time-consuming, CPU-intensive operation
 To reduce this burden: Pre-calculate paths
 Waypoint Navigation – Carefully place
nodes in the game environment, then use
pre-calculated paths or other inexpensive
methods to move between each node.
 Useful for both TBE and CE (plus point!)
 Example: Placing suitable nodes on a simple
map with 7 rooms
 What can you observe from these 7 nodes???
Waypoint Navigation
 Every node is in the line-of-sight of at least ONE node!
 Setting up like this, a NPC is able to reach every single room
in the world using simple line-of-sight algorithm
 Game AI just needs to know HOW the nodes are connected
to one other
Waypoint Navigation
 Using node labels and links, we can now determine a path
from any room to any room
 Example: Moving from room with node A to room with
node E  Move following ABCE path
Waypoint Navigation
 Can these different paths between rooms be PRE-calculated
beforehand?
 If NO, WHY? If YES, HOW?
Waypoint Navigation
 To move from node to node, determine the node that is
nearest to the player’s location and in player’s line-of-sight
 Use a lookup table to store data of shortest paths between
any two nodes
 Establish the connections between nodes
 Left side: Starting nodes
 Top side: Ending nodes
 Determine best path by looking at
intersection on the table between starting
and ending nodes
Waypoint Navigation
 We can discover the connections and fill in the table either 1)
manually or 2) by simulated traversal of nodes
 Example: Moving from A to all other nodes resulted in B as
the nearest node
Waypoint Navigation
 Continue doing this until the entire node connection table is
completed
Waypoint Navigation
 Example: Determine path from node B (triangle) to node G
(square) by repeatedly finding the intersection, starting with
nodes B and G. Using the intersected nodes as the subsequent
locations to move on
Waypoint Navigation – Post-mortem
 Question 1: What are some drawbacks of this method?
 Question 2: Are there ways to improve this current method
of waypoint navigation
Next…
 A* Algorithm  An extremely popular pathfinding
algorithm used widely in games
 Map Representations for A* Pathfinding (Grids, Polygonal
Maps, Navigation Meshes, Hierarchical)
Download