Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path finding for games

Tags:

path-finding

What are some path finding algorithms used in games of all types? (Of all types where characters move, anyway) Is Dijkstra's ever used? I'm not really looking to code anything; just doing some research, though if you paste pseudocode or something, that would be fine (I can understand Java and C++).

I know A* is like THE algorithm to use in 2D games. That's great and all, but what about 2D games that are not grid-based? Things like Age of Empires, or Link's Awakening. There aren't distinct square spaces to navigate to, so what do they do?

What do 3D games do? I've read this thingy http://www.ai-blog.net/archives/000152.html, which I hear is a great authority on the subject, but it doesn't really explain HOW, once the meshes are set, the path finding is done. IF A* is what they use, then how is something like that done in a 3D environment? And how exactly do the splines work for rounding corners?

like image 939
Pojo Avatar asked Apr 22 '12 18:04

Pojo


1 Answers

Dijkstra's algorithm calculates the shortest path to all nodes in a graph that are reachable from the starting position. For your average modern game, that would be both unnecessary and incredibly expensive.

You make a distinction between 2D and 3D, but it's worth noting that for any graph-based algorithm, the number of dimensions of your search space doesn't make a difference. The web page you linked to discusses waypoint graphs and navigation meshes; both are graph-based and could in principle work in any number of dimensions. Although there are no "distinct square spaces to move to", there are discrete "slots" in the space that the AI can move to and which have been carefully layed out by the game designers.

Concluding, A* is actually THE algorithm to use in 3D games just as much as in 2D games. Let's see how A* works:

  1. At the start, you know the coordinates of your current position and your target position. You make an optimistic estimate of the distance to your destination, for example the length of the straight line between the start position and the target.
  2. Consider the adjacent nodes in the graph. If one of them is your target (or contains it, in case of a navigation mesh), you're done.
  3. For each adjacent node (in the case of a navigation mesh, this could be the geometric center of the polygon or some other kind of midpoint), estimate the associated cost of traveling along there as the sum of two measures: the length of the path you'd have traveled so far, and another optimistic estimate of the distance that would still have to be covered.
  4. Sort your options from the previous step by their estimated cost together with all options that you've considered before, and pick the option with the lowest estimated cost. Repeat from step 2.

There are some details I haven't discussed here, but this should be enough to see how A* is basically independent of the number of dimensions of your space. You should also be able to see why this works for continous spaces.

There are some closely related algorithms that deal with certain problems in the standard A* search. For example recursive best-first search (RBFS) and simplified memory-bounded A* (SMA*) require less memory, while learning real-time A* (LRTA*) allows the agent to move before a full path has been computed. I don't know whether these algorithms are actually used in current games.

As for the rounding of corners, this can be done either with distance lines (where corners are replaced by circular arcs), or with any kind of spline function for full-path smoothing.

In addition, algorithms are possible that rely on a gradient over the search space (where each point in space is associated with a value), rather than a graph. These are probably not applied in most games because they take more time and memory, but might be interesting to know about anyway. Examples include various hill-climbing algorithms (which are real-time by default) and potential field methods.

Methods to procedurally obtain a graph from a continuous space exist as well, for example cell decomposition, Voronoi skeletonization and probabilistic roadmap skeletonization. The former would produce something compatible with a navigation mesh (though it might be hard to make it equally efficient as a hand-crafted navigation mesh) while the latter two produce results that will be more like waypoint graphs. All of these, as well as potential field methods and A* search, are relevant to robotics.


Sources:

  • Artificial Intelligence: A Modern Approach, 2nd edition
  • Introduction to The Design and Analysis of Algorithms, 2nd edition
like image 64
Julian Avatar answered Nov 22 '22 06:11

Julian