2PathfindingDifferent 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?
3PathfindingYou have heard of the A* Algorithm, the most popular & famous pathfinding algorithmBut 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
4Basic PathfindingThe 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
5Basic PathfindingOK, 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?
6Obstacles? Problems with obstacles Random Movement Obstacle Avoidance Simple and effective methodWorks well in environments with relative few obstaclesif Player In Line of SightFollow Path to PlayerelseMove in Random Direction
7Tracing Around Obstacles Another simple method – Tracing Around ObstaclesWhen encounter obstacle, switch to “tracing” stateTracing follows the edge of the obstacle to work way around it
8Tracing Around Obstacles Another simple method – Tracing Around ObstaclesWhen encounter obstacle, switch to “tracing” stateTracing follows the edge of the obstacle to work way around itBasic Tracing Movement:
9Tracing 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 destinationContinue Tracing until that line is crossed, then revert back to L-o-S pathfindingImproved Tracing:
10Tracing Around Obstacles Incorporate line-of-sight with tracing methodAt each step of tracing state, utilize line-of-sight to determine if a straight line-of-sight path can be followed to reach destination immediatelyIf a line-of-sight path is possible, switch back to line-of-sight pathfinding stateTracing with Line-of-Sight:
11Breadcrumb 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 worldBreadcrumb trail:
12Breadcrumb Pathfinding When the NPC encounters a breadcrumb, it simply begins to follow the trail until the endIn 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!
13Breadcrumb Pathfinding ImplementationBegin by creating a trail row and column arrays and setting each element value to -1Checks 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
14Following the breadcrumbs Example: Troll moves randomly (8 possible directions)Loop through the trail locations to determine if the troll has moved into a breadcrumb locationIf a breadcrumb is found, set the troll to use the path
15Following the breadcrumbs Due to possibility that the player’s path overlap itself or adjacent to previous location in pathNot Smart: NPC ends up taking exact footsteps of playerSolution: Allow NPC to always look for adjacent tile containing the most recent breadcrumb, skipping over breadcrumbs
16Path Following (in TBE) To confine a NPC to a certain terrain element such as road, we can use terrain labelingExample: Labeling road terrain tiles as 2s and other out-of- bounds terrain as 1s
17Path Following (in TBE) We do NOT want to make it move randomly allow the road tiles UnnaturalFrom 8 possible directions to move, eliminate those that are not part of the road. Then, decide on which of the remaining directions to takeSet neighboring road tiles to a big number and those out-of- bounds to 0.Tip: Keep the NPC moving in the same general direction, turn only when have toTip: Assign a number to each direction
18Path Following (in TBE) Weigh the directions so that priority will be given to maintaining previous directionExample: Current direction: 1Traverse the direction array in search for the most highly weighted direction, move to that direction in the next step
19Path Following (in TBE) How do we increase the robustness of this path following movement (to look more natural and intelligent)?
20Wall Tracing Does not calculate path from a starting to ending point Most useful in game environments with many rooms or mazesObstacle tracing (discussed earlier) can also be used to trace wallsRandom movement in such environments is commonly used to increase uncertainty but NPCs often get stuck in small rooms for long periods of time
21Wall TracingHow to make the troll EXPLORE and move in a systematic way around this environment?
22Wall Tracing Simple solution Left-handed approach If the NPC always move to the left, it will do a thorough job exploring the environmentMove LEFT WHENEVER POSSIBLE (Remember: Left of the NPC, not the player!)Example: NPC facing player’s rightDirection 2 is the NPC’s LEFTIf that is blocked try STRAIGHT, then try RIGHTIf still blocked, choose to reverse BACK
23Wall TracingImplementation: 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!
24Waypoint NavigationPathfinding Time-consuming, CPU-intensive operationTo reduce this burden: Pre-calculate pathsWaypoint 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 roomsWhat can you observe from these 7 nodes???
25Waypoint NavigationEvery 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 algorithmGame AI just needs to know HOW the nodes are connected to one other
26Waypoint NavigationUsing node labels and links, we can now determine a path from any room to any roomExample: Moving from room with node A to room with node E Move following ABCE path
27Waypoint NavigationCan these different paths between rooms be PRE-calculated beforehand?If NO, WHY? If YES, HOW?
28Waypoint NavigationTo move from node to node, determine the node that is nearest to the player’s location and in player’s line-of-sightUse a lookup table to store data of shortest paths between any two nodesEstablish the connections between nodesLeft side: Starting nodesTop side: Ending nodesDetermine best path by looking at intersection on the table between starting and ending nodes
29Waypoint NavigationWe can discover the connections and fill in the table either 1) manually or 2) by simulated traversal of nodesExample: Moving from A to all other nodes resulted in B as the nearest node
30Waypoint NavigationContinue doing this until the entire node connection table is completed
31Waypoint NavigationExample: 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
32Waypoint 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
33Next…A* Algorithm An extremely popular pathfinding algorithm used widely in gamesMap Representations for A* Pathfinding (Grids, Polygonal Maps, Navigation Meshes, Hierarchical)