Click here to load reader

Pathfinding

Embed Size (px)

DESCRIPTION

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? - PowerPoint PPT Presentation

Citation preview

Pathfinding

Basic MethodsPathfindingPathfindingDifferent 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?PathfindingYou 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

Basic PathfindingThe most simple, easiest and fastest solution to pathfinding is to re-use the Chase movementbut 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 path4Basic 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?

5Obstacles?Problems with obstaclesRandom Movement Obstacle AvoidanceSimple and effective methodWorks well in environments with relative few obstacles

if Player In Line of Sight Follow Path to Playerelse Move in Random Direction

Tracing Around ObstaclesAnother simple method Tracing Around ObstaclesWhen encounter obstacle, switch to tracing stateTracing follows the edge of the obstacle to work way around it

Tracing Around ObstaclesAnother simple method Tracing Around ObstaclesWhen encounter obstacle, switch to tracing stateTracing follows the edge of the obstacle to work way around itBasic Tracing Movement:

Tracing Around ObstaclesWe 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:

Tracing Around ObstaclesIncorporate 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:

Breadcrumb PathfindingAble 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:

Breadcrumb PathfindingWhen 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! Breadcrumb PathfindingImplementationBegin by creating a trail row and column arrays and setting each element value to -1Checks for the players direction key presses and records them down dropping a breadcrumbSince there is a max trail length, the oldest position will be dropped so that a new position can be addedFollowing the breadcrumbsExample: 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

Following the breadcrumbsDue to possibility that the players 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

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 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

Path 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

Path Following (in TBE)How do we increase the robustness of this path following movement (to look more natural and intelligent)?

Wall TracingDoes not calculate path from a starting to ending pointMost 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

Wall TracingHow to make the troll EXPLORE and move in a systematic way around this environment?

Wall TracingSimple solution Left-handed approachIf 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 players rightDirection 2 is the NPCs LEFTIf that is blocked try STRAIGHT, then try RIGHTIf still blocked, choose to reverse BACK

Wall 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!

Waypoint NavigationPathfinding Time-consuming, CPU-intensive operationTo 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 roomsWhat can you observe from these 7 nodes???

Waypoint 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

Waypoint 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

Waypoint NavigationCan these different paths between rooms be PRE-calculated beforehand? If NO, WHY? If YES, HOW?

Waypoint NavigationTo move from node to node, determine the node that is nearest to the players location and in players 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

Waypoint 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

Waypoint NavigationContinue doing this until the entire node connection table is completed

Waypoint 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

Waypoint Navigation Post-mortemQuestion 1: What are some drawbacks of this method?Question 2: Are there ways to improve this current method of waypoint navigation

NextA* Algorithm An extremely popular pathfinding algorithm used widely in gamesMap Representations for A* Pathfinding (Grids, Polygonal Maps, Navigation Meshes, Hierarchical)