240 likes | 348 Views
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
E N D
CSE 380 – Computer Game ProgrammingPathfinding 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 • For an object at point p1, suggest an algorithm to get to point p2
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
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
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
Real Time Pathfinding • Modern strategy games use lots of units simultaneously • Dynamically computing paths can be expensive • Solution: precompute
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
Key for precomputed paths • Paths should avoid obstacles
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
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
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?
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?
Depth-first search • Searches one way all the way until: • it finds the goal OR • it runs out of space
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
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
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
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
References • A* Pathfinding for beginners • http://www.policyalmanac.org/games/aStarTutorial.htm