Additional Design Patterns for Games For CSE 3902 Matt Boggus.
Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest...
-
Upload
regina-joseph -
Category
Documents
-
view
218 -
download
2
Transcript of Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest...
![Page 1: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/1.jpg)
Pathfinding and mazes
CSE 3541Matt Boggus
![Page 2: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/2.jpg)
Pathfinding – problem statement
• Finding the shortest route between two points
• How to represent the environment
• Lots of algorithms
![Page 3: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/3.jpg)
Environment representation
• Agents need to know where they can move• Search space should represent either– Clear routes that can be traversed– Or the entire walkable surface
• Search space typically doesn’t represent:– Small obstacles or moving objects
• Most common search space representations:– Grids– Waypoint graphs– Navigation meshes
![Page 4: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/4.jpg)
Grids
![Page 5: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/5.jpg)
Grids
![Page 6: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/6.jpg)
Grids as Graphs
![Page 7: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/7.jpg)
Distance metrics
![Page 8: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/8.jpg)
Waypoint graph
![Page 9: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/9.jpg)
Constructing waypoint graphs
Sampling discussion and boardwork
![Page 10: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/10.jpg)
Navigation mesh
![Page 11: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/11.jpg)
Navigation mesh with adjacency graph
![Page 12: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/12.jpg)
Pathfinding algorithms
• Random walk• Random trace• Breadth first search• Best first search• Dijkstra’s algorithm• A* algorithm
![Page 13: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/13.jpg)
Evaluating graph search algorithms
• Quality of final path• Resource consumption during search– CPU and memory
• Whether it is a complete algorithm– A complete algorithm guarantees to find a path if
one exists
![Page 14: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/14.jpg)
Random walk
• Agent takes a random step– If goal reached, then done
• Repeat procedure until goal reached
• Add intelligence– Only step in cases where distance to goal is smaller– Stuck? With a certain probability, allow a step where
distance to goal is greater
![Page 15: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/15.jpg)
Random trace
• Agent moves towards goal– If goal reached, then done– If obstacle• Trace around the obstacle clockwise or counter-
clockwise (pick randomly) until free path towards goal
• Repeat procedure until goal reached
![Page 16: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/16.jpg)
Random walk and trace properties
• Not complete algorithms
• Found paths are unlikely to be optimal
• Consume very little memory
![Page 17: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/17.jpg)
Random walk and trace performance
![Page 18: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/18.jpg)
Search algorithms
• Breadth-First• Best-First• Dijkstra’s• A* (combination of Best-First and Dijkstra)
• Nodes represent candidate paths• Keep track of numerous paths simultaneously
![Page 19: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/19.jpg)
Graph search complete algorithms
• Logically, these algorithms use two lists: open and closed
• Open list keeps track of unexplored nodes
• Examine a node on the open list:– Check to see if it is the goal– If not, check its edges to add more nodes to the open list
• Each added node contains a reference to the node that explored it
– Place current node on closed list (mark a field as visited)
• Closed list are those that have been explored/processed and are not the goal
![Page 20: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/20.jpg)
Implementation
1. Create start point node and push onto open list2. While open list is not empty
A. Pop node from open list (call it currentNode)B. If currentNode is the goal, break from step 2C. Create new nodes (successors nodes) for cells
around currentNode and push them onto open listD. Put currentNode onto closed list
// How to compute the solution path?
![Page 21: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/21.jpg)
Node selection• Breadth-First processes the node that has been waiting the longest
– Uses a queue
• Best-First processes the one that is closest to the goal– Uses a priority queue –node value is distance to goal
• Dijkstra’s processes the one that is the cheapest to reach from the start cell – Uses a priority queue – node value is lowest cost from the start
• A* chooses a node that is cheap and close to the goal– Uses a priority queue – node value is a combination of Best-First and
Dijkstra’s
![Page 22: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/22.jpg)
Breadth first search
![Page 23: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/23.jpg)
Breadth first search in a grid
• Represent environment using a grid of integers that encode spatial information
• int grid[x,y]– Initial values:
• For starting position of search, grid[x,y] = 0• For impassable grid cells, grid[x,y] = -1• Everywhere else, grid[x,y] = INFINITY
– After the search:• grid[x,y] = distance from start
-1 if impassable INFINITY if unreachable from start
![Page 24: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/24.jpg)
Flood-fill
• Determine the area connected to a given node in a multi-dimensional array
• Applications:– “Paintbucket” tool– Connected components– Pathfinding
Animation fromhttp://en.wikipedia.org/wiki/Flood_fill
![Page 25: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/25.jpg)
Recursive Flood-fill codeFlood-fill (x, y, value, grid) { if(x < 0 || y < 0 || x >= XSIZE || y >= YSIZE) return; if(grid[x,y] == -1 || grid[x,y] == value) return;
grid[x,y] = value; Flood-fill(x-1,y, value, grid); Flood-fill(x+1,y, value, grid); Flood-fill(x,y+1, value, grid); Flood-fill(x,y-1, value, grid);}
Note: method is prone to stack overflow
![Page 26: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/26.jpg)
Iterative Flood-fill algorithmFlood-fill (x, y, value, grid) { Set Q to the empty queue Add position(x,y) to Q While Q is not empty { Dequeue position p if (p.x or p.y out of bounds) continue; if (grid[p.x,p.y] == -1 || grid[p.x,p.y] == value) continue; grid[p.x,p.y] = value; Enqueue (p.x-1,p.y); Enqueue (p.x+1,p.y); Enqueue (p.x,p.y-1); Enqueue (p.x,p.y+1); }
![Page 27: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/27.jpg)
Queue vs. Stack Flood-fills
Animations from http://en.wikipedia.org/wiki/Flood_fill
![Page 28: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/28.jpg)
Flood-fill with varying value
Flood-fill (x, y, value, grid) { if(x < 0 || y < 0 || x >= XSIZE || y >= YSIZE)
return; if(grid[x,y] == -1 || grid[x,y] <= value)
return; grid[x,y] = value; Flood-fill(x-1,y, value+1, grid); Flood-fill(x+1,y, value+1, grid); Flood-fill(x,y+1, value+1, grid); Flood-fill(x,y-1, value+1, grid);}
![Page 29: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/29.jpg)
Result: distance field (8 connected)
![Page 30: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/30.jpg)
Result: distance field (8 connected)
![Page 31: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/31.jpg)
Best first search
![Page 32: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/32.jpg)
Suboptimal best first search
![Page 33: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/33.jpg)
Dijkstra’s algorithm
• Disregards distance to goal– Keeps track of the cost of every path– No guessing / no search heuristic
• Computes accumulated cost paid to reach a node from the start
• Uses the cost (called the given cost) as a priority value to determine the next node that should be brought out of the open list
![Page 34: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/34.jpg)
Dijkstra search
![Page 35: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/35.jpg)
A* algorithm
• Admissible heuristic function that never overestimates the true cost
• To order the open list, use– Heuristic cost – estimated cost to reach the goal– Given cost – cost to reach node from the start
Final Cost = Given Cost + (Heuristic Cost * Heuristic Weight)
![Page 36: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/36.jpg)
Heuristic weight
• Final Cost = Given Cost + (Heuristic Cost * Heuristic Weight)
• Heuristic weight controls the emphasis on the heuristic cost versus the given cost
• Controls the behavior of A*– If hw=0, final cost will be the given cost ->Dijkstra– As hw increases, it behaves more like Best-First
![Page 37: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/37.jpg)
A* search
![Page 38: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/38.jpg)
A* and Dijkstra comparison
![Page 39: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/39.jpg)
More reading on pathfinding
• Amit’s A* Pages http://theory.stanford.edu/~amitp/GameProgramming/
• Beginner’s guide to pathfinding http://ai-depot.com/Tutorial/PathFinding.html
• Waypoint graphs vs. navigation meshes http://www.ai-blog.net/archives/000152.html
![Page 40: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/40.jpg)
Mazes
Maze taken from Image-guided maze construction, Xu and Kaplan 2007, Siggraph
![Page 41: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/41.jpg)
Simple mazes – binary grid
• Matrix of booleans• ex: 21 x 21
• Arbitrary mapping:– True = black pixel = wall– False = white pixel =
open space
![Page 42: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/42.jpg)
Game mazes– enumerated grid
• Matrix of enumerated values : ints
• Arbitrary mapping– 0 = white pixel = open
space– 1 = black pixel = wall– 2 = health = red plus– Etc…
![Page 43: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/43.jpg)
Procedural maze initial values
All open space – add walls All walls – add open space
Advanced algorithms can also start with artist input or an image
![Page 44: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/44.jpg)
Maze generation – start with all walls
• Randomized depth first search– Mark current node as visited– Make list of neighbors, pick one randomly– If new node is not marked as visited, remove the
wall between it and its parent– Recurse
![Page 45: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/45.jpg)
Maze generation – start with all walls
• Randomized Kruskal's algorithm– Randomly select a wall that joins two
unconnected open areas, and merge them– For an n x n grid there are initially n2
open areas, one per grid cell
• Randomized Prim's algorithm– Randomly pick a starting point– Keep track of walls on the boundary of the maze– Randomly select a wall that “grows” the maze
![Page 46: Pathfinding and mazes CSE 3541 Matt Boggus. Pathfinding – problem statement Finding the shortest route between two points How to represent the environment.](https://reader036.fdocuments.in/reader036/viewer/2022081516/56649f2f5503460f94c48dcb/html5/thumbnails/46.jpg)
Examples
Animation examples fromhttp://en.wikipedia.org/wiki/Maze_generation_algorithm