Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Navigation and waynding in videogames

Dimas Tichelaar March 8th, 2012


Abstract This paper discusses pathnding algorithms, the A* algorithm and its implementation in video games. Dierent kinds of virtual worlds will be analyzed and implementations of the A* algorithm in these dierent kinds of virtual worlds will be given and explained. A number of guidelines for natural and logical path planning will be given with user experience in mind.

Introduction

Most video games with virtual environments or virtual worlds that can be navigated through will incorporate entities that have the ability to move around these virtual worlds. These entities will have to be programmed in such a way that they can move through these virtual worlds with some kind of purpose. This purpose can be simple like adding a bit of ambience to a game environment, like animals walking around in Starcraft 2, but there are more complicated purposes for navigation like enemies in Halo spreading out to ank the players position and gain a better angle of attack. There is a virtually innite amount of applications for pathnding algorithms in video games and how it is used will depend solely on what the developers want it to be. This paper will contain an overview of the most widely accepted pathnding algorithm, A*, and how it can be applied and extended in dierent types of virtual worlds and situations.

Pathnding algorithms: A*

In order to achieve any form of a useful pathnding algorithm one must rst look at the very basics of what a pathnding algorithm is supposed to achieve. The simplest way to describe a pathnding problem is to dene a 1

2D plane with a starting point, a goal and optional obstacles between the starting point and the goal. A method for moving an entity from start to goal would be to move the entity towards the goal while checking if an obstacle is closely in front of it, blocking the path from the entity towards the goal. If there is no obstacle in the way the entity will reach their goal in a straight path, but should an obstacle be in the way, the entity would trace along the edges of the obstacle (and repeat the process in case of multiple obstacles) until a clear path to the goal is found. While this is a simple approach to navigation in 2D, it is very inecient in most situations. If we look at Figure 1, we see how this approach relates to an approach that calculates the optimal trajectory beforehand. An algorithm used for nding the shortest path from start to goal is Dijkstras algorithm.

Figure 1: A concave obstacle between start and goal. In general, pathnding algorithms work with graphs, sets of vertices with edges connecting them. Dijkstras algorithm checks the closest not-beforechecked vertices starting from the start point, and adds the neighbouring vertices to the set of vertices it needs to check and saving the travel cost from the starting point per vertex. It repeats the entire process, expanding outwards, until it reaches the goal. After this the shortest path can be found by nding the neighbouring vertex with the lowest travel cost from the start, and repeating this process until it reaches the start point. While Dijkstras algorithm will always return a shortest path (there can be multiple shortest 2

paths), it becomes less and less ecient as the total amount of vertices the algorithm has to check increases more and more as the distance from the starting point increases. In a 2D plane with horizontal and vertical edges, this goes by the following formula:
d

4ni
i=1

where d is the distance (in tiles) from the start to the goal vertex. An alternative to Dijkstras algorithm would be the Greedy Best-First-Search algorithm, which works like Dijkstras algorithm but uses a heuristic function to nd out which vertices are in the direction of the goal and only expands those vertices. While the Greedy Best-First-Search algorithm will do less work to nd a shortest path to the goal in situations without obstacles, it will not return the shortest path when an obstacle is in the way due to the algorithm moving towards the goal even when the distance does not turn out to be the shortest distance in the end, as displayed in Figure 2.

Figure 2: Left: Dijkstras algorithm performance with an obstacle. Right: Greedy Best-First-Search algorithm performance with an obstacle. Ideally, we would want to use an algorithm that uses the heuristics of the Greedy Best-First-Search algorithm and the shortest path results of Dijkstras algorithm. This is exactly what the A* algorithm does. A* uses a heuristic function that provides an estimated distance from a vertex to the goal. In this standard case, the heuristic function can work in a few ways: If h(n) is 0, the algorithm will not direct towards the goal resulting in Dijkstras algorithm. 3

If h(n) is lower than the cost of moving from start to the goal, then the algorithm is guaranteed to return a shortest path. If h(n) is equal to the cost of moving from start to the goal, then A* will return the perfect shortest path without any deviation. If h(n) is sometimes greater than the cost of moving from start to the goal, A* isnt guaranteed to return a shortest path. If h(n) is way higher than the cost of moving from start to the goal, A* will only take the direction towards the goal into consideration and the algorithm turns into Best-First-Search. Depending on the choice made for the heuristics function, A* can run very fast or very slow with results varying from no shortest path in the worst case to always the shortest path in the best case. It is however highly likely that the algorithm will cause the game to run very slowly in games that require the calculation of pathnding for a lot of entities at the same time. This can be avoided in a few ways, namely using a smaller map, fewer units, spreading out pathnding calculations over multiple cycles, designing pre-calculated paths, pre-processing the map or tagging nodes that end in a dead end accordingly. Following is the pseudocode for an implementation of A*:
function A*(start,goal) closedset := the empty set // The set of nodes already evaluated. openset := {start} // The set of tentative nodes to be evaluated, initially containing the start node came_from := the empty map // The map of navigated nodes. g_score[start] := 0 // Cost from start along best known path. h_score[start] := heuristic_cost_estimate(start, goal) f_score[start] := g_score[start] + h_score[start] // Estimated total cost from start to goal through y. while openset is not empty current := the node in openset having the lowest f_score[] value if current = goal return reconstruct_path(came_from, came_from[goal]) remove current from openset add current to closedset foreach neighbor in neighbor_nodes(current) if neighbor in closedset continue tentative_g_score := g_score[current] + dist_between(current,neighbor) if neighbor not in openset add neighbor to openset h_score[neighbor] := heuristic_cost_estimate(neighbor, goal) tentative_is_better := true else if tentative_g_score < g_score[neighbor] tentative_is_better := true else

tentative_is_better := false if tentative_is_better = true came_from[neighbor] := current g_score[neighbor] := tentative_g_score f_score[neighbor] := g_score[neighbor] + h_score[neighbor] return failure function reconstruct_path(came_from, current_node) if came_from[current_node] is set p := reconstruct_path(came_from, came_from[current_node]) return (p + current_node) else return current_node

Virtual world representations

While the A* pathnding algorithm can easily be applied to tile-based game environments, there is a variety of types of game worlds that can be used in videogames. Examples include a tileless 2D space (Spore), tile based 3D (Starcraft 2) and polygonal 3D (The Elder Scrolls V: Skyrim) to name a few. These types of game worlds are substantially dierent from one another, but the general rules of pathnding algorithms still apply, albeit with a dierent implementation. We have discussed the usage of the algorithm for strictly 2D tile based game worlds in section 2. Tile based 2.5D or 3D games can dier from tile based 2D games in the obvious extra dimension. Depending on the implementation, one might want to increase or lower cost values for dierent kinds of tiles because of a dierence in height, or restrict movement between dierent height levels. In Starcraft 2, most ground units can only move between game areas with dierent heights through ramps, but the Colossus unit is able to walk across these clis, meaning an exception must be made for units with such an ability. Implementing a pathnding algorithm like A* becomes more complicated when used in a 3D world solely consisting of polygons, which is very common. A method for achieving proper navigation routes in 3D worlds would be to insert waypoints throughout the world, but that method has a variety of issues. Depending on the size of worlds, which can be very big (GTA IV, World Of Warcraft) a lot of waypoints would need to be inserted in order to guarantee entity movement across the entire world. Besides that, entities can only move from waypoint to waypoint, resulting in choppy movement which is often frowned upon (more on that in section 5). A more viable solution for pathnding in expansive 3D worlds would be the creation of navigation meshes. Navigation meshes are meshes which designate areas that can be walked upon by entities, with the option to store additional information 5

relevant to the navigation mesh. These meshes are created about as easily as creating normal meshes in your editor of choice and oer a wider range of nodes for entities to use. Dierent types of movement over navigation meshes include: Polygon movement, where entities use the mesh centers as waypoints, Polygon edge movement, where entities can move from edge to edge, Polygon vertex movement, where entities can travel along the vertices, Hybrid movement, where entities can use multiple parts of polygons as waypoints.

Customising A*

One of the reasons why A* is such a widely implemented pathnding algorithm is its versatility. As discussed before, vertices arent limited to representation by a grid based system and the speed and precision of the algorithm can be adjusted by changing the heuristics function depending on what is most suitable for the game. However, one might want to factor in more situational variables for the pathnding algorithm to account for, like moving groups of entities together, avoiding hilly or hazardous areas, staying away or engaging enemies whenever possible and so on. An implementation of a custom A* is described in Limited Damage A* by S. Bayili & F. Polat, where A* is used to account for threat sources in pathnding. In their report they describe using a heuristics formula h(x) = DiCF hdi (x) + DCF hd (x) for an A* algorithm, with hdi (x) being the distance heuristic estimate, hd (x) being the damage heuristic estimate, DiCF being the Distance Contribution Factor and DCF being the Damage Contribution Factor. According to the heuristics function entities are likely to avoid tiles which contain enemies that pose a threat to the entities themselves, resulting in a pathnding algorithm that takes limited damage per entity. This is just one very clear example of how A* can be altered to suit the needs of the developer and the game involved.

Natural motion and user experience

Pathnding in videogames is incredibly important when it comes to making sure that a game is well playable and the user experience is optimal. In order to keep a sense of ow or immersion in a videogame, the movement 6

of entities must be signicantly uent. The shortest path from a start to a goal with one or multiple obstacles in the way would result in a jagged path, but the resulting motion and possibly animation would be notably choppy. In order to avoid this Planning the shortest paths or the least damaging paths is a big aspect of pathnding, but one must take into account the way a player looks at the execution of the entities moving along these found paths and whether the pathnding makes sense in the rst place. There is a number of ways that pathnding could detract from an enjoyable game experience. Entities are not supposed to be too dumb, meaning that entities should have a signicant amount of sense of location and goal in a game. If entities get stuck in corners, in between polygons or any similar kind of deadlock caused by a faulty pathnding result players will get agitated fast and lose their interest in the game. On the other hand, entities shouldnt be too smart when it comes to pathnding.

Figure 3: A scouting unit from Starcraft 2 discards the ramp as the shortest path while the unit is unaware of the block at the top of the ramp. A clear example of this can be found in Starcraft 2, where scouting is an integral part of the game. A players base will be situated on a higher 7

ground, and the only way to get onto that higher ground is to travel up a ramp, which is easily blocked. If a ramp is blocked and a unit is sent to go up on the platform, the unit will not go up the ramp and instead hug the walls of the platform, even though neither the player or the unit has ever observed the ramp being blocked.

Conclusion

When it comes to navigation in video games, there is a plethora of techniques available to implement the optimal form of navigation. A* appears to be the most dynamic and versatile algorithm for path planning in virtual worlds and it is usable for all dierent kinds of virtual worlds. The usage of heuristics ensures performance the way the developers want it to perform and external factors for pathnding.

References
[1] B. Flynn, Languages Of Navigation Within Computer Games, paper presented to the Digital Arts Conference, 2003. [2] D. Nieuwenhuisen, A. Kamphuis, M.H. Overmars, High Quality navigation in computer games, Science Direct, 2007. [3] S. Kang, Y. Kim, C. Kim, Live path: adaptive agent navigation in the interactive virtual world, Springer-Verlag, 2010. [4] C. Xiao, S. Hao, Direction oriented pathnding in video games, Intelligence & Applications (IJAIA), Vol.2, No.4, October 2011. [5] R.E. Korf, Heuristic Evaluation Functions in Articial Intelligence Search Algorithms, Minds and Machines, 1995. [6] S. Bayili, F. Polat, Limited-Damage A*: A path search algorithm that considers damage as a feasibility criterion, Knowledge-Based Systems, Vol.24, No.4, pp.501-512 May 2011. [7] M. Santos, V. Lopez, Dyna-H: A heuristic planning reinforcement learning algorithm applied to role-playing game strategy decision systems, Cornell University Library, 2011. 8

[8] S. Burigat, L. Chittaro, Navigation in 3D virtual environments: Eects of user experience and location-pointing navigation aids, International Journal of Human-Computer Studies 65, Elsevier, 2007. [9] B. Balakrishnan, S.S. Sundar, Where am I? How can I get there? Impact of Navigability and Narrative Transportation on Spatial Presence, Human-Computer Interaction, Taylor & Francis, 2011. [10] R. Meintema, Navigating Virtual Worlds, University of Groningen, 2011. [11] R.P. Darken, B. Peterson, Spatial orientation, waynding and representation, Handbook of Virtual Environment Technology, 2001. [12] I. Millington, J. Funge, Articial Intelligence for Games, Elsevier, 2009. [13] R.C. Holte, M.B.P Perezm R.M. Zimmer, A.J. MacDonald Hierarchical A*: Searching Abstraction Hierarchies Eciently 1996 [14] http://www-cs-students.stanford.edu/~amitp/gameprog.html [15] http://en.wikipedia.org/wiki/A*_search_algorithm [16] http://www.gamedev.net/page/resources/_/technical/ artificial-intelligence/a-pathfinding-for-beginners-r2003 [17] http://www.gamedeveloperskills.com/?p=13

You might also like