1 / 24

CSE 380 – Computer Game Programming Pathfinding AI

CSE 380 – Computer Game Programming Pathfinding AI. Dig Dug, by Namco. Pathfinding. Computation and execution of a path from point p1 to p2 Perhaps most common AI problem in games Can be very frustrating to implement well Algorithms must be tailored to fit games

satya
Download Presentation

CSE 380 – Computer Game Programming Pathfinding AI

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. CSE 380 – Computer Game ProgrammingPathfinding AI Dig Dug, by Namco

  2. Pathfinding • Computation and execution of a path from point p1 to p2 • Perhaps most common AI problem in games • Can be very frustrating to implement well • Algorithms must be tailored to fit games • For an object at point p1, suggest an algorithm to get to point p2

  3. Simple 2D Vectoring Solution • Determine vector v to head towards p2 v.x = p2.x – p1.x v.y = p2.y – p1.y • Now, scale v according to speed (use simple geometry) • What’s the problem? • we are assuming there are no obstacles

  4. Simple Trial and Error • For simple obstacles that aren’t large • Algorithm: • Go towards target destination • When you hit an obstacle: • Back it up • Randomly turn it right or left 45-90 degrees • Move it forward again for some random amount in a range • Go back to step 1 • Easy & fast • Not good for fixed obstacles of any size

  5. Contour Tracing • Algorithm: • Go towards target destination • If you hit an obstacle: • trace the contour of the obstacle blocking the path • move such that you are a uniform distance from edge • periodically test if a line to your destination intersects the obstacle anymore • if yes, stop tracing and head towards destination • if no, go back to tracing

  6. Real Time Pathfinding • Modern strategy games use lots of units simultaneously • Dynamically computing paths can be expensive • Solution: precompute

  7. Waypoint Pathfinding • Setup a network of paths • connect all points of interest in the game via a connected network of nodes • each node represents a waypoint • edges in the network represent: • vector direction • vector length

  8. Key for precomputed paths • Paths should avoid obstacles

  9. What are precomputed paths? • Nodes: • locations on map (x, y) • Edges • direct paths to other nodes (x, y) • distance • Vectors • computed from node to node along path • Options: • pre-compute and store entire paths (only viable for small data sets) • dynamically calculate paths from pre-computed graph data

  10. How do we improve finding a path • Game objects don’t necessarily start at nodes • they shouldn’t have to go to nodes to pick up a path either • Made easier by path coverage • making sure that everywhere on the board can reach a path quickly

  11. Dynamic Pathfinding Algorithms using Graphs • Breadth-first search • Depth-first search • Dijkstra’s algorithm • A* • Premise: • you have a graph of nodes • you are at one node & you want to get to another • What combination of edges will get you there?

  12. Breadth-first search • Fan out in all directions at the same time • visit each node one unit away • then two units away • then three • etc. • Like a growing circle • What’s bi-directional breadth-first search?

  13. Depth-first search • Searches one way all the way until: • it finds the goal OR • it runs out of space

  14. Dijkstra’s Algorithm • Similar to minimum spanning tree algorithm • O(V2), where V is the # of vertices • Premise: • find the shortest path from starting node to other nodes along the way • use those shortest path to determine your ultimate shortest path • A nice Dijkstra’s Applet: • http://carbon.cudenver.edu/~hgreenbe/sessions/dijkstra/DijkstraApplet.html

  15. A* Algorithm • Solves shortest path problem for a directed graph • All nodes have a G & H value • G: min distance from origin node (A) to the given node • H: estimated distance to goal • Key to determining which nodes to use: • Minimize G + H

  16. How does it work • Place origin node A in open list • Look at all adjacent nodes for A • Add them to open list with A as parent • Remove A from open list & add to closed list • Which node (B) is the minimum distance (G + H)? • Remove minimum from open list and add to closed list • Look at all adjacent nodes for B not on the closed list • Add them to open list with B as parent • Which node (C) is the minimum distance (G + H)? • Is that node (C) already on the open list? • If yes, just ignore B in path • Continue in this manner until destination is reached

  17. A* and Grids • Strategy games are typically played on grids • A grid can be easily divided into large nodes • store the center of the node • nodes must not include impassible terrain (e.g. water) • The smaller the nodes, the more processing • Example assumptions: • horizontal/vertical movements cost 10 • diagonal movements cost 14 • no diagonal movements through obstacles

  18. A* Grid Example

  19. A* Grid Example

  20. A* Grid Example

  21. A* Grid Example

  22. A* Grid Example

  23. A* Grid Example

  24. References • A* Pathfinding for beginners • http://www.policyalmanac.org/games/aStarTutorial.htm

More Related