By Patrick Lester ( Updated January 9, 2003)
In my main article, A* Pathfinding for Beginners, I described A* in very general terms, and described how to create a single all-purpose pathfinding function. Creating only one pathfinding function, however, can be needlessly limiting.
Consider the following RPG situation, and a swordsman who wants to pathfind around a nearby wall:
Given this kind of map, you could place nodes in a variety of ways, and use a variety of densities. In this example, let's use a high-density node network, as is shown below.
In this graphic, the white nodes are walkable. Spots where there are no nodes are unwalkable. We also do some things a little differently in this example. In this example you are allowed to "cut corners" around unwalkable squares. Also the "squares" themselves aren't square anymore. Because this is an isometric example, we have decided to pack twice as many nodes on the Y (vertical axis) as there are on the X (horizontal) axis. This allows proper isometric paths.
As you can see, using this tightly packed node network, we can pathfind not only around the nearby wall but also between the wall and the nearby barrel in the process. Our dense node network also allows pathfinding around the standing torch, where the node at the very base is unwalkable. This, in sum, is precision pathfinding.
Well, that is pretty cool in short-distance situations, but what do we do if we need to pathfind across the entire map? Using a densely packed node network like this could easily leave you searching through 10,000+ nodes on just a single pass through the A* loop. That will bring just about any PC to a grinding halt.
So let's look at an alternative. What if we chose to create a much less dense node network, like the one you see below?
In this example, the nodes are in the center of the large isometric diamonds. Data about walkability between the diamonds is stored in the same array along the edges, and is represented in this graphic with small red dots. To move from one isometric tile to one of its 8 adjacent macro-tiles, the adjacent tile must itself be walkable, and the path between must not be blocked.
This node network is 72 times less dense than the earlier one. Instead of dealing with as many as 10,000+ nodes at any given time, here we will only be dealing with 1/72 of that, or less than 200. Our computer can definitely handle that. Pathfinding across the map is no longer a problem.
Putting the Two Together
So, which method do we choose? Both!
On any given search, you would first pathfind across the map, using the macro-level pathfinder. You would then switch to the micro-pathfinder and search for a path from the current location to the node two macro-nodes away on the path. Once you entered each new macro-level diamond "square," you would use the micro-level pathfinder to pathfind to the macro-node two macro nodes ahead. This ends up wasting the second half of each micro-path, but you need to do this to make sure it looks good -- and it isn't much of a waste, since you are micro-pathfinding across a relatively short distance. Once you get 2-3 macro nodes away from the target, you would switch over completely to the micro-pathfinder, and pathfind to the final location.
Using this approach, you will get speed pathfinding across the entire map and be able to negotiate around corners, barrels, etc. in a realistic way, as in the earlier micro-pathfinding example. Pretty cool, huh!
And if you are *really* ambitious, you could develop three pathfinders if you have a really big map, one at a really macro level, one medium level, and one micro-level. The professionals have done this for games like Age of Empires. It just depends on what your needs are.
Well, that's it. As always, I can be reached at:
Until then, good luck!