24
CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Embed Size (px)

Citation preview

Page 1: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

CSE 380 – Computer Game ProgrammingPathfinding AI

Dig Dug, by Namco

Page 2: 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

• For an object at point p1, suggest an algorithm to get to point p2

Page 3: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Simple 2D Vectoring Solution

• Determine vector v to head towards p2v.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

Page 4: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Simple Trial and Error

• For simple obstacles that aren’t large• Algorithm:

1. Go towards target destination2. 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

3. Go back to step 1

• Easy & fast• Not good for fixed obstacles of any size

Page 5: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Contour Tracing

• Algorithm:1. Go towards target destination

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

Page 6: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Real Time Pathfinding

• Modern strategy games use lots of units simultaneously

• Dynamically computing paths can be expensive

• Solution: precompute

Page 7: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 8: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Key for precomputed paths

• Paths should avoid obstacles

Page 9: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 10: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 11: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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?

Page 12: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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?

Page 13: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

Depth-first search

• Searches one way all the way until:– it finds the goal

OR– it runs out of space

Page 14: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 15: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 16: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 17: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

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

Page 18: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 19: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 20: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 21: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 22: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 23: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

A* Grid Example

Page 24: CSE 380 – Computer Game Programming Pathfinding AI Dig Dug, by Namco

References

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