1 / 34

Pathfinding

Pathfinding. A* Algorithm and Map Representations. Pathfinding Superstar: A* Algorithm. A* Algorithm  One of the most, if not THE most used pathfinding algorithm in game dev today

afric
Télécharger la présentation

Pathfinding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pathfinding A* Algorithm and Map Representations

  2. Pathfinding Superstar: A* Algorithm • A* Algorithm One of the most, if not THE most used pathfinding algorithm in game dev today • Appeal: Almost guaranteed to find the best path between any starting point and ending point, assuming, that a path exists • Efficiency: Does not require complete exploration of all possible paths to arrive at a best path

  3. Ultimate algorithm? • If a clear line-of-sight with no obstacles exists, the A* algorithm may be overkill and inefficient • Consumption of processing can be demanding if we need to perform simultaneous pathfinding for a large number of game characters • However, in many cases, A* algorithm (and its variants) is the best choice

  4. A* Algorithm – Define Search Area • 1st Step: Define the Search Area • Define a way of representing the game world to allow the search algorithm to search for and find the best path • Game characters and objects must occupy “points” on the map • In TBE, tiles can be used as points • In CE, there is a LARGE number of points that characters/objects can occupy. So, A* is not practical UNLESS simplification of the search area or the points can be done…

  5. A* Algorithm – Define Search Area • For now, we assumed a TBE (Map representations will be discussed later) • Nodes can be placed throughout game world, so that paths can be build between nodes • Not necessarily between every possible point in world • Think: This simplification can also be implemented for CE!

  6. A* Algorithm – Define Search Area • Even this could be too many nodes. Nodes do not need to correspond directly to every possible tank position – can be reduced (simplify further!)

  7. A* Algorithm – Define Search Area • In a TBE, “points” on the world are not unreasonably large, so each tile can be regarded as a node in a search area • TBEs are well-suited for the A* algorithm • No need to maintain list of links because they are already adjacent in the game world • Cost of moving from one node to an adjacent node is also uniform (unless you regard the diagonal movements as 1.414 times the normal moving cost )

  8. A* Algorithm – Starting the Search • 2nd Step: Starting the Search • Example scenario • Starting node: Spider at centre • Ending node: Human location • Solid black tiles: Wall obstacles • White tiles: Walkable areas • Main Idea: • Search begins at the starting node, and branches out to surrounding nodes, Repeat this branching until ending node is reached • Need to keep track which tiles have been searched

  9. A* Algorithm – Starting the Search • Main Idea (cont’d): • Whenever a node has been searched, add it to the open list. • Traverse the open list and continue searching nodes that are adjacent to each node on the list • Only valid nodes (walkable) are considered • Also, a closed list will contain nodes that have been checked and no longer needs to be examined. We add a node to this list once all its adjacent tiles have been checked

  10. A* Algorithm – Starting the Search • Main Idea (cont’d): • Also, a closed list will contain nodes that have been checked and no longer needs to be examined • We add a node to this list once all its adjacent tiles have been checked

  11. A* Algorithm – Starting the Search • Main Idea (cont’d): • We need to know how the adjacent nodes are link together  Track the parent node of each node in the open list • Example: Each node in the open list is “linked” to the starting tile as its parent node • A TREE structure is needed… • Ultimately, we will use the parent links to trace a path back to the starting node once we reach the ending node.

  12. A* Algorithm – Starting the Search • Question: At each branching step, how do we determine which adjacent tile (which are now nodes in the open list) to check and “move to” ?

  13. A* Algorithm – Scoring • A score is applied to each tile. This “score” should give us an idea of which tile is the “best” choice (or best path towards destination) • Idea: There are 2 components involve in determining a “score” for a potential path when we are on a tile/node • Cost to move from starting node to current node • Cost to move from current node to ending node • Adding these two costs together to get total cost or “score” Starting node Current node Ending node Total path cost or Score

  14. A* Algorithm – Scoring • Score (at a given node) = Cost from Start + Cost to End • Cost of moving from initial location is easy (should already be known by summing cost taken to reach current position) • However, how do we determine the cost of moving from a given tile to the destination tile?? Guess??

  15. A* Algorithm – Scoring • Yes. Guess. • Make a guess, use a heuristic • Score (at a given node), f(n) = Cost from Start , g(n)+ Heuristic, h(n) • Heuristic is an estimate of the cost of getting from the given tile to the destination tile • The simplest heuristic that we can use is to estimate the number of steps from the given tile to the destination tile (w/o taking obstacles into consideration since we can’t see into the future if the obstacles already exist!)

  16. A* Algorithm – Heuristics • 2 types of heuristics: • Pre-computed exact heuristics: Pre-compute the length of the shortest path between every pair of points. Not feasible for most game maps. Remedy: Fit a coarse grid on top of fine grid • Linear exact heuristics: Assuming no obstacles, no slow terrain, just compute shortest path from start to goal in a straight line • Types of heuristic functions: • Manhattan distance (4-direction) • Diagonal distance or Chebyshev distance (8-direction) • Euclidean distance (any direction)

  17. A* Algorithm – Scoring • Now, calculated the total cost, f(n) for all adjacent (child) nodes of the current node. • Example: Determining total cost (using diagonal distance as heuristic) • Tile with the lowest total cost will equate to a shorter path, so this tile will be chosen first from the open list on the next iteration • We actually have THREE tiles with the lowest total cost of 4. Which to choose then? f=4 h=3 g=1

  18. A* Algorithm – Scoring • Simple rule: Choose any. It doesn’t matter (we will see later how certain paths that reach a dead end have to retract back..) • More complex: “Tie-breaking” methods (refer to Amit’s A* notes) • Let’s say we simply choose the upper right tile (5,6) to move to • Algorithm is repeated in the next iteration, to search for adjacent tiles that are a) not previously examined (not in open list) and, b) not obstacles

  19. A* Algorithm – Scoring • In this case, only 2 tiles (5,7) and (6,7) qualify • Add these two tiles to the open list • Calculate the total cost of these new tiles • Move node (5,6) to close list • Repeat the process again • Search the open list for the tile with the lowest cost, expand that tile and so on… g =2 h=3 f = 5 g =2 h=4 f = 6

  20. A* Algorithm – Scoring • Run through the steps again to continue the pathfinding process… • Note: It is good practice to keep an open and closed list nearby when you work this out

  21. A* Algorithm – Scoring • When we reach the destination, how does the algorithm determine when we reached it? • Iteration in algorithm ENDS when destination node/tile is added to the open list

  22. A* Algorithm – Scoring • The path is found by following the parent links back to the starting point • It is possible to find other paths of equal length

  23. A* Algorithm – Dead End • It is possible that no valid path exists between any two given points, so how do we know when we have reached a dead end? • If there are no more members in the open list to examine anymore, a dead end is reached

  24. A* Algorithm – Summary • Pseudocode add the starting node to the open list while the open list is not empty { current node=node from open list with the lowest cost if current node = goal node then path complete else move current node to the closed list examine each node adjacent to the current node for each adjacent node if it isn't on the open list and isn't on the closed list and it isn't an obstacle then move it to open list and calculate cost }

  25. Map Representations • Before A* Algorithm can be implemented, we need to know what type of map representation to use • “The map representation can make a huge difference in the performance and path quality”, Amit Patel, Stanford • Pathfinding algorithms are worse than linear • Doubling distance travel, it takes more than twice as long to find the path! • The fewer nodes you use, the faster A* will be • The closer the nodes match the positions that units move to, the better the path quality

  26. Map Representations • Grids • Uniform subdivision of the world into small “tiles” • Square, triangle, isometric, hexagonal, etc. • Within grids, you have a choice of different movements • Tile movement • Edge movement • Vertex movement

  27. Map Representations • Polygonal Maps • If movement cost across large areas are uniform AND if your units can move in straight lines instead of following grid, use this non-grid representation as alternative • OK to use non-grid graph for pathfinding on top of grid world • Navigation points are computed to build a visibility graph

  28. Map Representations • Visibility graphs can be a computational nightmare in maps with open areas or long corridors!

  29. Map Representations • Navigation Meshes • Represent walkable areas with non-overlapping polygons also called a navigation mesh • We can treat this much like how we treat grids  Choice of using polygon centers, edges or vertices as points

  30. Map Representations • Navigation Meshes – Determining points • The pink path shows the ideal smoothened path Polygon Edge Movement Polygon Vertex Movement Polygon Movement

  31. Map Representations • Hierarchical • A flat map has only one level of representation in a map, for e.g. tiles • Higher levels of representation can be carved out to define larger rooms, or buildings or entire worlds • Multiple levels of pathfinding can be combined in hierarchical manner • Example: An NPC finds a path from one location to another within a cave, then exits the cave and finds a path from the cave to another connected cave, and then exits the cave to find a path to the forest or city. Can you see the different levels of map representation?

  32. Map Representations • Hierarchical • A hierarchical map has many levels of representation • A heterogeneous hierarchy typically has a fixed number of levels, each with different characteristics  Putting many “layers” of worlds on top of one another

  33. Map Representations • Hierarchical • A hierarchical graph can be constructed to accommodate different levels • Connections between higher level nodes need to reflect the ability to move between lower-level grouped areas

  34. Next lesson • Implementing A* Pathfinding with… • Terrain Cost • Influence Mapping

More Related