0% found this document useful (0 votes)
37 views

A-Based Pathfinding in Modern Computer Games

Pathfinding in computer games has been investigated for many years. A* algorithm is probably the most popular pathfinding algorithm used in game industry. Since it was created, many researchers have worked to optimize A* algorithm for pathfinding in games. This paper reviews several popular A*-based pathfinding algorithms and techniques used to optimize A* algorithm from different perspectives such as improving heuristic methods, optimizing map representations, and reducing memory requirements. Real examples of how these pathfinding techniques are applied in modern computer games are also provided.

Uploaded by

kokszno2017
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

A-Based Pathfinding in Modern Computer Games

Pathfinding in computer games has been investigated for many years. A* algorithm is probably the most popular pathfinding algorithm used in game industry. Since it was created, many researchers have worked to optimize A* algorithm for pathfinding in games. This paper reviews several popular A*-based pathfinding algorithms and techniques used to optimize A* algorithm from different perspectives such as improving heuristic methods, optimizing map representations, and reducing memory requirements. Real examples of how these pathfinding techniques are applied in modern computer games are also provided.

Uploaded by

kokszno2017
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/267809499

A*-based Pathfinding in Modern Computer Games

Article · November 2010

CITATIONS READS

154 41,700

2 authors:

Xiao Cui Hao Shi


Victoria University Melbourne SOAS, University of London
3 PUBLICATIONS 173 CITATIONS 31 PUBLICATIONS 399 CITATIONS

SEE PROFILE SEE PROFILE

All content following this page was uploaded by Xiao Cui on 09 March 2015.

The user has requested enhancement of the downloaded file.


IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011 125

A*-based Pathfinding in Modern Computer Games


Xiao Cui and Hao Shi

School of Engineering and Science, Victoria University, Melbourne, Australia

Summary game. Considerable effort has been made to optimize this


Pathfinding in computer games has been investigated for many algorithm over the past decades and dozens of revised
years. It is probably the most popular but frustrating game algorithms have been introduced successfully. Examples
artificial intelligence (AI) problem in game industry. Various of such optimizations include improving heuristic
search algorithms, such as Dijkstra’s algorithm, bread first methods, optimizing map representations, introducing
search algorithm and depth first search algorithm, were created
to solve the shortest path problem until the emergence of A*
new data structures and reducing memory requirements.
algorithm as a provably optimal solution for pathfinding. Since The next section provides an overview of A* techniques
it was created, it has successfully attracted attention of which are widely used in current game industry.
thousands of researchers to put effort into it. A long list of
A*-based algorithms and techniques were generated. This
paper reviews a number of popular A*-based algorithms and 2. A* algorithm
techniques from different perspectives. It aims to explore the
relationship between various A*-based algorithms. In the first A* is a generic search algorithm that can be used to find
section, an overview of pathfinding is presented. Then, the solutions for many problems, pathfinding just being one
details of A* algorithm are addressed as a basis of delivering a of them. For pathfinding, A* algorithm repeatedly
number of optimization techniques from different angles.
examines the most promising unexplored location it has
Finally, a number of real examples of how the pathfinding
techniques are used in real games are given and a conclusion is
seen. When a location is explored, the algorithm is
drawn. finished if that location is the goal; otherwise, it makes
Key words: Pathfinding, A*, A* optimization, Computer game note of all that location’s neighbors for further
exploration. A* is probably the most popular path finding
algorithm in game AI (Artificial Intelligence) [2].
1. Introduction 1. Add the starting node to the open list.
2. Repeat the following steps:
Pathfinding generally refers to find the shortest route a. Look for the node which has the lowest
between two end points. Examples of such problems f on the open list. Refer to this node
include transit planning, telephone traffic routing, maze as the current node.
b. Switch it to the closed list.
navigation and robot path planning. As the importance of c. For each reachable node from the current
game industry increases, pathfinding has become a node
popular and frustrating problem in game industry. Games i. If it is on the closed list, ignore
it.
like role-playing games and real-time strategy games ii. If it isn’t on the open list, add it
often have characters sent on missions from their current to the open list. Make the current
location to a predetermined or player determined node the parent of this node. Record
destination. The most common issue of pathfinding in a the f, g, and h value of this node.
iii. If it is on the open list already,
video game is how to avoid obstacles cleverly and seek check to see if this is a better
out the most efficient path over different terrain. path. If so, change its parent to the
Early solutions to the problem of pathfinding in current node, and recalculate the f
computer games, such as depth first search, iterative and g value.
d. Stop when
deepening, breadth first search, Dijkstra’s algorithm, best i. Add the target node to the closed
first search, A* algorithm, and iterative deepening A*, list.
were soon overwhelmed by the sheer exponential growth ii. Fail to find the target node, and the
in the complexity of the game. More efficient solutions open list is empty.
3. Tracing backwards from the target node to the
are required so as to be able to solve pathfinding starting node. That is your path.
problems on a more complex environment with limited
time and resources. Fig. 1 Pseudocode of A* [3].
Because of the huge success of A* algorithm in path
finding [1], many researchers are pinning their hopes on In the standard terminology used when talking about A*,
speeding up A* so as to satisfy the changing needs of the g(n) represents the exact cost from starting point to any

Manuscript received January 5, 2011


Manuscript revised January 20, 2011
126 IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011

point n, h(n) represents the estimated cost from point n to roadmap of North America, showing all roads annotated
the destination, and f(n)=g(n)+h(n). Fig. 1 lays out the with driving distances, an A* implementation can
algorithm step-by-step. compute the optimal travel route but this might be an
A* has several useful properties which have been proved expensive computation because of the sheer size of the
by Hart, Nilsson and Raphael in 1968 [4]. First, A* is roadmap. However, a hierarchical path finding would
guaranteed to find a path from the start to the goal if never work at such a low level of detail. Using
there exists a path. And it is optimal if h(n) is an abstraction can quickly find a route. The problem
admissible heuristic, which means h(n) is always less described above might be solved more efficiently by
than or equal to the actual cheapest path cost from n to planning a large-scale route at the city level first and then
the goal. The third property of A* is that it makes the planning the inter routes at each city passing through.
most efficient use of the heuristic. That is, no search
method which uses the same heuristic function to find an
optimal path examines fewer nodes than A*.
Although A* is the most popular choice for pathfinding
in computer games, how to apply it in a computer game
depends on the nature of the game and its internal
representation of the world. For example, in a
rectangular grid of 1000×1000 squares, there are 1
million possible squares to search. To find a path in that
kind of map simply takes a lot of work. Thus, reducing
the search space may significantly speed up A*. Several
optimizations are discussed in Section 3.

3. A* Optimizations
Fig. 2 Five ways to represent search space [5].
The following sub-sections discuss several potential
optimizations of A* from four different perspectives and A much faster A*-based search algorithm giving nearly
reviews some popular A*-based algorithms. solutions named HPA* is described in [6]. This is a
domain-independent approach. The hierarchy can be
3.1 Search Space extended to more than two levels, making it more
scalable for large problem spaces. A three-step process is
In any game environment, AI characters need to use an applied. The first step is to travel to the border of the
underlying data structure – a search space representation neighborhood that contains the start location. Then, the
– to plan a path to any given destination. Finding the second step is to search for a path from the border of the
most appropriate data structure to represent the search start neighborhood to the border of the goal
space for the game world is absolutely critical to neighborhood. This step is done at an abstract level,
achieving realistic-looking movement and acceptable where search is simpler and faster. The last step is to
pathfinding performance. As you can see in the above complete the path by travelling from the border of the
example, a simpler search space will mean that A* has goal neighborhood to the goal position. HPA* has been
less work to do, and less work will allow the algorithm to proved that it is 10 times faster than a low-level A* in [6].
run faster. Examples of such representations include The potential problem of this technique is that the cost
rectangular grid (Fig. 2a), quadtree (Fig. 2c), convex increases significantly when adding a new abstraction
polygons (Fig. 2d), points of visibility (Fig. 2e), and layer.
generalized cylinders (Fig. 2f).
The following sub-sections review two popular A*-based 3.1.2 Navigation Mesh (NavMesh)
algorithms which optimize A* algorithm by reducing the
search space. NavMesh is another popular technique for AI pathfinding
in 3D worlds. A NavMesh is a set of convex polygons
3.1.1 Hierarchical Pathfinding A* (HPA*) that describe the “walkable” surface of a 3D environment.
It is a simple, highly intuitive floor plan that AI
Hierarchical pathfinding is an extremely powerful characters can use for navigation and pathfinding in the
technique that speeds up the pathfinding process. The game world.
complexity of the problem can be reduced by breaking Fig. 3b shows an example of NavMesh. A character
up the world hierarchically. Consider the problem of moves from the starting point in pol2 to the desired
travelling from Los Angeles to Toronto. Given a detailed destination in pol4. In this case, the starting point is not
IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011 127

in the same polygon as the desired point. Thus, the search using various heuristic costs while trying to
character needs to determine the next polygon it will go overcome a large obstacle.
to. Repeat this step until both the character and the goal When the heuristic equals to zero (shown in Fig. 4a), A*
are located in the same polygon. Then, the character can algorithm turns to Dijkstra’s algorithm. All the
move to the destination in a straight line. neighboring nodes are expanded. When the heuristic uses
Compared with a waypoint graph as shown in Fig. 3a, the Euclidean distance to the goal (shown in Fig. 4b),
NavMesh approach is guaranteed to find a near optimal only the nodes that look like better options are examined.
path by searching much less data. And the pathfinding When the heuristic is overestimated a little (shown in Fig.
behavior in a NavMesh is superior to that in a waypoint 4c), the search pushes hard on the closest nodes to the
graph [7]. goal. Thus, overestimating the heuristic cost a little may
result in exploring much fewer nodes than
non-overestimation heuristic approaches. However, how
much should the cost be overestimated is a tricky
problem. No general solution exists at present.

3.3 Memory
Although A* is about as good a search algorithm as you
can find so far, it must be used wisely; otherwise, it
might be wasteful of resources. A* algorithm requires a
huge amount of memory to track the progress of each
search especially when searching on large and complex
environments. Reducing the required memory for
pathfinding is a tricky problem in game AI. There has
been a lot of work on this area.

Fig. 3 Different representations of waypoint graph and NavMesh [7]

Creating navigation meshes that are highly simplified


and easy for pathfinding is critical to achieving a good
pathfinding. Tozour [9] describes how to construct a
good navigation mesh and proposes a number of
optimization techniques for navigation mesh.

3.2 Heuristic Function


The secret to the success of A* is that it extends
Dijkstra’s algorithm by introducing heuristic approach.
Dijkstra’s algorithm is guaranteed to find a shortest path
in a connected weighted graph as long as none of the
edges has a negative value but it is not efficient enough
because all the possible states must be examined.
However, A* algorithm improves the computational
efficiency significantly by introducing a heuristic
approach. Using a heuristic approach means, rather than
an exhaustive expansion, only the states that look like
better options are examined. The heuristic function used
in A* algorithm is to estimate the cost from any nodes on Fig. 4 Comparison between different heuristics [10].
the graph to the desired destination. If the estimated cost
is exactly equal to the real cost, then only the nodes on The most popular way to avoid memory waste is to
the best path are selected and nothing else is expanded. pre-allocate a minimum amount of memory [10]. The
Thus, a good heuristic function which can accurately general idea is to dedicate a piece of memory (Node
estimate the cost may make the algorithm much quicker. Bank) before A* starts execution. During the execution,
On the other hand, using a heuristic that overestimates if all the memory gets exhausted, create a new buffer to
the true cost a little usually results in a faster search with progress the search. The size of this buffer is allowed to
a reasonable path [10]. Fig. 4 shows the growth of the change so that less memory is wasted. The size of the
128 IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011

minimum memory mainly depends on the complexity of group of units goes around forest to get to another
the environment. Thus, tuning is required before this position, half of them get stuck in the trees as shown in
strategy is applied to a particular application. Fig. 5. Such situations always happen especially when
Another alternative to reduce space requirements in A* is the density of forest increases.
to compute the whole path in small pieces. This is the
core concept behind IDA* (Iterative Deepening A*),
which is a very popular variant of A* algorithm. In IDA*,
a path is cut off when its total cost f(n) exceeds a
maximum cost threshold. IDA* starts with a threshold
equal to f(start_node), in this case, the threshold is equal
to h(start_node) because g(start_node)=0. Then,
neighboring nodes are expanded until either a solution is
found that scores below the threshold or no solution is
found. In this case, the threshold is increased by one, and
another search is triggered. The main advantage of IDA*
over A* is that memory usage is significantly reduced.

3.4 Data Structure


Once a node has been initialized from the Node Bank
(see Section 3.3), it needs to be put somewhere for fast
retrieval. A hash table might be the best choice because it Fig. 5 A screenshot of Age of Empires II. [11]
allows constant time storing and looking up of data. This
hash table allows us to find out if a particular node is on Another strategy game Civilization V uses hexagonal
the CLOSED list or the OPEN list instantaneously. tiles to represent map locations as shown in Fig. 6. A
A priority queue is the best way to maintain an OPEN list. pathfinding algorithm is applied to control the military
It can be implemented by a binary heap. There is little unit moving to the desired location through a group of
work on introducing new data structures to maintain “walkable” hexagonal tiles. Similar to Age of Empires,
OPEN list and CLOSED list more efficiently. Probably Civilization V still suffers with bad pathfinding although
introducing a new data structure to store the data can it is the latest game of Civilization series which was
help speed up A* significantly. released in November 2010.

4. Relevant Applications in Computer


Games
As a popular pathfinding algorithm in game industry, A*
algorithm has been applied to a wide range of computer
games. Although the algorithm itself is easy to
understand, implementation in a real computer game is
non-trivial. This section discusses several popular
computer games in terms of pathfinding and uses a
popular online game as an example to show how the
different map representations can impact on the Fig. 6 A screenshot of Civilization V. [12]
performance of pathfinding.
However, compared with strategy games which involve
4.1 Pathfinding Challenge in Game Industry hundreds and thousands of units simultaneously, A*
works much better in first-person shooter games like
Age of Empires is a classic real-time strategy game. It Counter-Strike which only involves a few units moving
uses grids to represent map locations. A 256×256 grid around at the same time. An explanation might be that
yields 65,536 possible locations. The movement of the the exponential growth in the number of units moving
military unit can be simplified as if moving an object around at the same time makes the game environment
through a maze. A* algorithm is applied to Age of much more dynamic and it is hard to provide optimal
Empires. Although it looks perfect theoretically, many paths for hundreds and thousands of units in real time
Age of Empires players are annoyed by the terrible using limited CPU and memory resources. Massively
pathfinding. An example of such problems is that when a
IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011 129

multiplayer online is another example which involves techniques described in this paper have been widely used
real-time pathfinding intensively, like World of in current game industry. The reason why they are
WardCraft. reviewed in this paper is that they are the hottest topics in
the academic domain of pathfinding and many
4.2 Comparison between Map Representations researchers are struggling to bring them into real games.
It is expected that this research help game industry has a
Waypoint graph is a popular technique to represent map basic understanding about the future research direction in
locations. Waypoints are a set of points that refer to the pathfinding.
coordinates in the physic space. It is designed for
navigation purpose and has been applied to a wide range
of areas. Most game engines support pathfinding on a
waypoint graph. Although it works well for most 2D
games, it requires tons of waypoints to achieve an
adequate movement in a 3D game because of the
complexity of the environment. Thus, a new technique
called NavMesh is created. As mentioned in Section
3.1.2, NavMesh only requires a couple of polygons to
represent the map. It results in a much more quickly
pathfinding because less data is examined.
Five reasons why NavMesh works better than waypoint
approaches in 3D games using World of WarCraft as an
example are addressed by Tozour in 2008 [8]. It shows
the difference between waypoints approaches and
NavMesh when representing a complex 3D environment.
It uses the town of Halaa in World of WarCraft as an (a) Navigating from A to B using waypoint graph.
example as shown in Fig. 7. In Fig. 7a, it uses 28
waypoints to represent the possible locations while on
the other hand, as shown in Fig. 7b, only 14 convex
polygons are used. The movement in Fig. 7b also acts
much more like an actual human than the movement in
Fig. 7a.

5. Conclusion
This paper systematically reviews several popular
A*-based algorithms and techniques according to the
optimization of A*. It shows a clearly relational map
between A* algorithm and its variants. The core of
pathfinding algorithm is only a small piece of the puzzle
in game AI. The most challenge is how to use the
algorithm to solve tricky problems. A* algorithm is the (b) Navigating from A to B on NavMesh.
most popular algorithm in pathfinding. It is hard-pressed
Fig. 7 Comparison between wapoing graph and NavMesh [8].
to find a better algorithm since A* is provably optimal. A
lot of effort has been put into speeding it up by
References
optimizing it from different perspectives. The ways to [1] B. Stout, “Smart moves: intelligent path-finding,” in Game
improve the performance of A* search include Developer Magazine, pp.28-35, 1996.
optimizing the underlying search space, reducing the [2] Stanford Theory Group, “Amit’s A* page”,
memory usage, improving heuristic functions and http://theory.stanford.edu/~amitp/GameProgramming/ASta
introducing new data structures. rComparison.html, accessed October 12, 2010.
A potential research is to continue optimizing A* [3] N.Nilsson, Artificial Intelligence: A New Synthesis,
algorithm from these perspectives or to combine multiple Morgan Kaufmann Publishers, San Francisco, 1998.
optimization techniques into one single solution. Another [4] P. Hart, N. Nilsson, and B. Raphael, “A formal basis for
the heuristic determination of minimum cost paths,” IEEE
way to make some contribution to the game AI
Trans.Syst.Sci.Cybernet., vol.4, no.2, pp.100-107, 1968
community is to apply these techniques described above
to the real computer games because not all of the
130 IJCSNS International Journal of Computer Science and Network Security, VOL.11 No.1, January 2011

[5] B. Stout, “The basics of A* for path planning,” in Game


Programming GEMS, pp.254-262, Charles River Meida,
America, 2000.
[6] A.Botea, M.Mueller, and J.Schaeffer, “Near optimal
hierarchical path-finding,” J. GD, vol.1, no.1, pp.7-28,
2004.
[7] Unreal Developer Network, “Navigation mesh reference”,
http://udn.epicgames.com/Three/NavigationMeshReferenc
e.html, accessed Jan.13, 2011.
[8] Game/AI, “Fixing pathfinding once and for all”,
http://www.ai-blog.net/archives/000152.html, accessed
September 23, 2010.
[9] P. Tozour, “Building a near-optimal navigation mesh,” in
AI Game Programming Wisdom, pp.171-185, Charles
River Media, America, 2002.
[10] S. Rabin, “A* speed optimizations,” in Game
Programming GEMS, pp.264-271, Charles River Media,
America, 2000.
[11] Microsoft Games, “Microsoft Age of Empires”,
http://www.microsoft.com/games/empires, accessed
October 24, 2010.
[12] Firaxis Games, “Sid Meier’s Civilization V”,
http://www.civilization5.com, accessed October 24, 2010.

Xiao Cui is a Ph.D student in School


of Engineering and Science at Victoria
University, Australia. He completed his
master degree in the area of Software
Engineering at Australian National
University and obtained his Bachelor of
Computer Science degree at Victoria
University. His research interests
include Object-Oriented Software
Engineering, Pathfinding Algorithms,
Database Management and Game Programming.

Hao Shi is an Associate Professor in


School of Engineering and Science at
Victoria University, Australia. She
completed her PhD in the area of
Computer Engineering at University of
Wollongong and obtained her Bachelor
of Engineering degree at Shanghai Jiao
Tong University, China. She has been
actively engaged in R&D and external
consultancy activities. Her research
interests include p2p Network, Location-Based Services, Web
Services, Computer/Robotics Vision, Visual Communications,
Internet and Multimedia Technologies.

View publication stats

You might also like