Efficient Triangulation-Based Pathfinding: Douglas Demyen and Michael Buro
Efficient Triangulation-Based Pathfinding: Douglas Demyen and Michael Buro
942
a) Polygon World b) Triangulated World
fewer nodes are searched, much like with the h-value. How-
ever, the g-value — and by extension the sum of this and
the h-value — must be no greater than their true values if
the anytime algorithm is to converge on the optimal solu-
tion. This algorithm works as follows: we search as usual
until we find a path to the goal, we run the modified fun-
nel algorithm described later to determine the actual cost of
this path, and we continue searching, calculating lengths of
paths found and updating our best path each time we find
one shorter, until our best path is no greater than the sum of
c) Triangle Graph d) Abstract Triangle Graph
the g- and h-values of the remaining search nodes, at which
point we know we have the shortest possible path.
In order to achieve the highest possible underestimate for
the g-value, we take the maximum of a number of known
admissible values: the Euclidean distance between the start
and the closest point to it on the entry edge of the current
triangle; the distance between the start and the goal minus
this node’s h-value; the parent node’s g-value plus the dif-
ference between its h-value and that of the current node; and
the parent node’s g-value plus the shortest path between its
entry edge and that of the current triangle. All these combine
to create a fairly accurate g-value without TRA* having to
query individual triangles.
Figure 1: How a reduced triangulation graph is constructed
It turned out that a large portion of the time in the above
from a polygonal world description
method was spent finding the triangle containing the start-
ing point. This is because the point location used in (Kall-
mann 2005) simply “walked” from a fixed starting triangle
by avoiding thin triangles where to get between two edges, progressively closer to the point. To help speed this up, we
an object would have to exit and re-enter through the other. implemented a sector-based method wherein we overlaid a
In its simplest form, pathfinding in a constrained trian- grid of points on the triangulation and determined in which
gulation is done by hopping from triangle to triangle. This triangle each was contained. This allowed the point location
presents a challenge because we do not know the path the to begin “walking” from the triangle around the closest grid
object will take through each triangle. In (Kallmann 2005), point, resulting in much better performance. For the experi-
states are considered the midpoint of a triangle edge, their ments reported later, we used a modest grid size of 10 × 10.
children the midpoints across the triangle, and the object’s
path is made up of straight-line segments between these. In Non-Point Objects
addition, each visited triangle is marked so it cannot be vis- It is often the case in games that moving objects have some
ited again. This method can result in suboptimal paths, but size that must be considered while pathfinding. This is usu-
in practice performs well. Since we are concerned with pro- ally handled by means of “growing” the obstacles in the
ducing optimal results where we do not have to pay much of environment corresponding to the size of the objects and
a penalty, we take a different approach. pathfinding within this environment as with a point object.
Triangulation A*, or TA*, is our triangulation pathfinding While this approach has been shown to work, it has a draw-
algorithm. Similar to above, we consider the states in the back in that this environment must be calculated for each
search to be triangles and their children to be triangles adja- size (and shape) of object. This may not be singly disad-
cent across unconstrained edges. However in order to avoid vantageous if there are few such object footprints, however
pruning the optimal solution, we made some changes. if there are many or we desire to find a path for an object
Because we do not know the actual distance through the of arbitrary size, the time for such a calculation and the can
sequence of triangles until reaching the goal, we must con- become unwieldy.
sider all paths to a triangle in order to obtain the optimal so- If the environment is being represented as a grid, more
lution. When considering a triangle in either TA* or TRA* problems can arise due to the fact that it is often unnatural
(Triangulation Reduction A*, described later), we estimate to represent objects by way of grid squares. For example,
the cost incurred (the g-value) and the heuristic (the h-value) a circular object is often represented by a square or a “ras-
from any point on the entry edge of the triangle. The heuris- terized” circle, which are imprecise. If, on the other hand,
tic is calculated as the Euclidean distance between the goal the environment is triangulated, objects that are circular (or
and the closest point to it on this edge. We know this heuris- otherwise radially symmetrical) can be added around the ob-
tic to be both admissible and consistent, which is helpful stacles by use of the Minkowski Sum (by approximation to
since the incurred cost must also be estimated. a regular polygon), however this leads to a large number of
The estimation of the g-value as it turns out is critical in thin, sliver-like triangles which complicate pathfinding and
the efficiency of the search. The higher this value is, the have other undesirable effects on triangulations. In both
943
a) Triangle width b) Channel of triangles this, we construct a channel (Kallmann 2005), a simple poly-
start gon with the start and goal positions as vertices and which
C traces the perimeter of the triangles in between (Fig. 2b).
a Once we have this channel, we wish to find the shortest
b path within it to use for object motion. For a point object,
d B this can be found in time linear in the number of triangles in
the channel with what is called the funnel algorithm (Hersh-
A c berger & Snoeyink 1994). Obviously, since we are dealing
with larger objects, we desire an algorithm which can find
goal the shortest path within a channel, while keeping at least
distance r from the vertices of the channel (with the excep-
c) Funnel algorithm d) Example path
tion of the start and goal vertices). It turns out that with
some small modifications to the funnel algorithm, we can
find such a path also in linear time. We assume that such a
path is possible; if a channel cannot yield a valid path for an
object of radius r, it would not be considered by the search.
The original funnel algorithm is described in (Hersh-
berger & Snoeyink 1994), which we will not cover for lack
of space. The simple modification to accommodate circular
objects with radius r is conceptually adding circles of radius
r around each interior vertex, as in Fig. 2c. When consid-
ering the angle between two vertices, a segment tangent to
the circles around the vertices is used instead of a segment
Figure 2: Object radius considerations
connecting the vertices.
The result of this algorithm is a path consisting of arcs
around vertices and line segments tangent to these arcs, be-
cases, object shapes that aren’t roughly radially symmetri-
tween them, as shown in Fig. 2d. This produces the opti-
cal are seldom used in practice because of the complexity it
adds to the problem. mal path within the channel for an object of the given ra-
dius. However, this path requires that the object be capable
of curved motion, so when this is not available, it can be
Width Calculation
approximated by straight segments.
Triangulations offer a unique opportunity to accommodate
differently sized and shaped objects. At the time of writing, Triangulation Graph Reductions
the focus was on circular objects because of their promi- Each triangle in the triangulation is mapped to a single
nence in games. However, we believe with some calculation, “node” in the abstract graph. These nodes are categorized by
other shapes could be managed as well. When considering a degree between 0 and 3, inclusive, which refer to the num-
path for an object through a triangle, one must simply check ber of adjacent graph structures. Specifically, a triangle is
that the “width” through the triangle between the entry and mapped to a degree-n node when 3 − n of its edges are such
exit edges be large enough to accommodate that object (for that: either the edge is constrained or the triangle across that
a circular object, it must be twice the object’s radius). This edge is mapped to a degree-1 node.
width, when traveling between (unconstrained) edges a and We will cover a brief example based on the graph in Fig. 3
b of a triangle (see Fig. 2a), can be calculated as the closest to illustrate how these nodes are categorized. First we go
obstacle to vertex C in the region between the rays Ca and through the triangles in the graph to determine which of
Cb, where an obstacle is either a vertex in the triangulation, them have one or fewer unconstrained edges. In this ex-
or a point on a constrained edge. ample, the two triangles at the top of the “Y” structure have
One can check that a width of 2r through a triangle be- one unconstrained edge each since they have only one adja-
tween edges a and b is necessary and sufficient for a path to cent triangle. These are mapped to degree-1 nodes. If any
exist for an object of radius r between those edges in that were encountered with no unconstrained edges, these would
triangle. This eliminates the need for a separate representa- be mapped to degree-0 nodes.
tion of the environment for each size of object. While in the Next, we put the triangle to which these are adjacent on
worst case determining the width of one triangle is linear in to a queue for processing. We go through this queue and
the number of triangles in the environment, the properties of find that the triangle next to the top left one now has only
a triangulation make it likely that calculating the widths of one adjacent triangle not mapped to a degree-1 node, so this
all triangles in the environment will be similarly linear. triangle is mapped to a degree-1 node and the triangle at the
fork is added to the queue. This process continues until the
Modified Funnel Algorithm queue is empty. We will notice that this happens when the
A search through a triangulation does not yield a particular triangle at the base of the “Y” is reached: it has two adjacent
path, but a series of adjacent triangles with the start position triangles not mapped to degree-1 nodes. If a connected com-
in the first triangle and the goal position in the last. From ponent of the triangulation is acyclic (i.e. the triangle graph
944
lower bound on the distance to that triangle to help estimate
g-values, and the narrowest point between the triangles. We
also record the width between pairs of unconstrained edges
through the triangle.
Both the triangulation itself, plus all the abstraction in-
formation can be stored in an average of less than 183 bytes
per triangle, so that even the largest maps in our experiments
could be stored in around 3MB, the majority of those tested
in under 1MB, and if the environments were designed for
triangulation, considerably less. This is an important con-
sideration, especially for games.
Further Reductions
There are extensions that are possible for the graph and ab-
Figure 3: Typical triangulation graph and its reduced form. straction layers. Further abstraction is achievable if one col-
lapses doubly-connected components of the abstract graph
into single nodes of a more abstract graph. Where on the
forms a tree), this component’s triangles will all be mapped abstract graph the nodes represent decision points for which
to degree-1 nodes, since they will “collapse” from all sides. way to go around an obstacle, in this new graph, they would
represent “rooms” in the environment which contain multi-
At this point we go through the other triangles in this
ple paths between their entry and exit points.
graph and find unmapped triangles with no constrained This graph would then be a tree of doubly-connected
edges or adjacent triangles mapped to degree-1 nodes. These components. Because it is a tree, the highest level of the
are mapped to degree-3 nodes, and then we cross each edge
pathfinding problem would become trivial as there is only
and visit each adjacent unmapped triangle in turn, mapping one path between any two points in a tree. The best paths
them to degree-2 nodes, until no such node is adjacent or the between each pair of entry points of each doubly-connected
triangle qualifies to be mapped to a degree-3 node.
component could even be cached, after which the search
In this way, the edges of the graph at the bottom of Fig. 3 function would only have to get from the start and goal
would be formed, the triangles on them being mapped to points on to this new abstracted graph.
degree-2 nodes. The degree-3 nodes form the vertices of this
graph. Any remaining unmapped triangles map to degree-2 Abstracted Triangulation Searches
nodes, since they would form one or more “rings”. We here introduce TRA*, which searches the abstraction
Degree-n nodes are connected to n adjacent graph struc- described above. While these algorithms both consider
tures. Since all degree-0 nodes are based on triangles triangles as states, TA* generates children as the trian-
with all constrained edges, they aren’t adjacent to anything. gles adjacent across unconstrained edges, and TRA* gen-
Degree-1 nodes are adjacent to a degree-2 node, which we erates degree-3 nodes adjacent across degree-2 corridors.
call the “root”. For example the degree-1 nodes in the “Y” This greatly benefits search because the number of degree-3
in Fig. 3 are adjacent to the degree-2 node at the bottom of it. nodes is only dependent on the number of obstacles (there
The exception is when the triangulation graph is acyclic, as are 2n − 2 for n obstacles) and not their features.
described above, when the degree-1 nodes in this unrooted TRA* also has many cases for which no real search must
tree aren’t adjacent to anything. take place. For example, if both the start and the goal are
Degree-2 nodes are adjacent to degree-3 nodes, as shown in an unrooted degree-1 tree or if they are both in a tree
in Fig. 3 where degree-2 nodes on an edge are adjacent to the rooted at the same degree-2 node, we can find the path using
degree-3 nodes that form the endpoints of that edge. Again, a simple search. This search can consider just the midpoints
if the degree-2 nodes form a “ring”, this is an exception and of the triangles and only expand each node once, since we
they aren’t adjacent to anything. Finally, degree-3 nodes are know that in a tree, there is only one path between any two
adjacent to other degree-3 nodes either directly adjacent or points. Therefore, once we have a channel between the start
across chains of degree-2 nodes, of which there must be 3. and goal points, it must contain the shortest path between
This algorithm presented is linear in the number of trian- them and we can stop. If either the start or goal is in the root
gles in the triangulation. As well, in the case that the envi- triangle of a tree containing the other, we can simply walk
ronment changes, and the triangulation is repaired locally as from the tree along the single path to the root.
in (Kallmann, Bieri, & Thalmann 2003), the abstract graph If both are on the same degree-2 edge or in trees rooted
can be repaired locally using in the best case the triangles in the same edge, we form one path by walking between
that were modified, and in the worst case, the connected them on that edge, and then consider the start and goal to
component that was modified. be the degree-3 endpoints of that edge and continue with the
The nodes in the abstract graph, in addition to containing degree-3 search to check for any shorter paths. If, however,
the adjacent graph structures, also contain spatial informa- those endpoints are the same degree-3 node, we form an-
tion to be used in searching the graph. Each node contains other path across that node to compare with the other path
information on the adjacent node (if any) in each direction, a and take the shortest. Similarly if both are on a degree-2
945
ring, we form a paths going each way around the ring and A* Execution Time PRA* Execution Time
take the shortest. Only if none of these cases hold do we 300 30
95% 95%
need to perform a search of the degree-3 nodes. 250 75% 25 75%
The search of the degree-3 nodes is performed as follows. Median Median
Time (millisec)
200 25% 20 25%
If the goal is on a triangle mapped to a degree-1 or 2 node, 150
5%
15
5%
the goals of the search are considered to be the degree-3 end- 100 10
points of the edge on which the goal lies or the tree con-
50 5
taining the goal is rooted, otherwise it is simply the node
0 0
mapped to by the triangle containing the goal. Likewise, the 0 100 200 300 400 500 0 100 200 300 400 500
queue for the search algorithm is initialized with either the A* path length A* path length
node mapped by the triangle on which the start lies if that is TA* Execution Time (F=1) TRA* Execution Time (F=1)
degree-3, or those adjacent to it if not. Search then proceeds 12 1.6
as before moving between the degree-3 nodes of the abstract 10
95% 1.4
95%
75%
75%
graph, with the same considerations as TA*. 1.2 Median
Time (millisec)
Median
8 25% 1 25%
5% 5%
Experiments 6 0.8
0.6
4
While one could argue that our technique is not compara- 0.4
2
ble to grid-based pathfinding techniques, this type of en- 0.2
0 0
vironment offers both a plethora of maps used in commer- 0 100 200 300 400 500 0 100 200 300 400 500
cial games and fast algorithms with which to compare ours. A* path length A* path length
Hence we took this opportunity to see if TA* and TRA*
could beat these other techniques “at their own game”. Figure 4: Time percentiles for A*, PRA*, TA*, and TRA*
For comparison sake, we used the exact same maps, and
start and goal points as in the PRA* experiment presented
in (Sturtevant & Buro 2005). The data was kindly made in search, it was not able to calculate as accurate g-values as
available by the authors. In the interest of simplicity, we TA*. As a result, TRA* was sometimes led astray more and
also used the results from that experiment, with the excep- also could not prove its solution to be optimal as quickly.
tion of halving the execution times to adjust for the different Due to the aforementioned estimations and the need to
CPU speeds. The set of maps consisted of 75 Baldur’s Gate consider all paths to each node, one thing that posed a
maps — a grid of tiles marked traversable or untraversable problem to the triangulation searches was the presence of
— and 41 WarCraft III maps — a grid of different types many, especially small obstacles — this was most apparent
of terrain and heights in which paths cannot cross height in the WarCraft III maps, which were often sprinkled with
differences without ramps, or boundaries between different trees. Besides creating more triangles for TA* to search and
types of terrain. All maps were scaled to 512 × 512, while degree-3 nodes for TRA* to search, this forced them to con-
maintaining their connectivity. Each map contained 1280 sider a large number of paths to each node. In the case of
paths with length between 0 and 511, 10 in each of 128 small obstacles, these paths differed very little in their g-
“buckets” (a path with length l belonging to bucket i ex- values, keeping the searches from pruning them.
actly when i = bl/4c). Both TA* and TRA* were run on Because of this, a small fraction of searches would take
the total 148480 paths over 116 maps on a computer with an several times longer than the others. Since these would not
AMD Athlon64 3200+ processor and 1GB of RAM using be acceptable in a real-time setting, we modified TA* and
Microsoft Visual Studio .NET 2003. As with A* and PRA*, TRA* slightly to not expand a triangle twice until the first
neither TA* or TRA* have been highly optimized for speed. path was found. This allowed for the first paths to be found
Both TA* and TRA* performed admirably in almost all much faster, without affecting subsequent paths.
cases. Despite not benefiting from the abstraction infor- The path quality of the anytime algorithms are shown in
mation, TA* still found most paths faster than the grid- Fig. 6. The F values refer to the multiple of the time to find
based methods (see Fig. 4 and note the scale on the time the initial path at which the statistics were taken. The graphs
axes). This is a testament to the usefulness of triangu- show the 75th and 95th percentiles for the path lengths for
lations for reducing the number of nodes in the environ- the algorithms, divided by the length of the path found by
ment. One can also note that because the environments were TA* with F = 10, or TA*(10). This was chosen because in
grid-based, non-axis-aligned barriers were approximated by most cases, TA*(10) was optimal, but to address the times
jagged edges, and there were more triangles in the environ- when it was not, the “bound” line was added to these graphs.
ment than if it was built using off-axis segments. Since we know that the A* paths were optimal when con-
Of course, TRA* does not suffer from this effect, its size strained to a grid, this line represents the minimum length a
only depending on the number of obstacles, not their shape. path could have without this constraint. The bound for the
As a result, TRA* performed better overall, as seen in Fig. 4, nth percentile is calculated as the (100 − n)th percentile of
demonstrating the effectiveness of the abstraction on the tri- the A* length, divided by a constant C ≈ 1.0824 which rep-
angulation, reducing the effective number of nodes in the resents how much longer a grid-based path could possibly
environment even further. One advantage TA* had over be over its arbitrary motion equivalent.
TRA* is that since TRA* skipped between degree-3 nodes We see when looking at the bound that TA*(10) is defi-
946
Median Speedup over A* Node expansions (90-th perc.) We see when comparing the TA* and TRA* performance
180 250 in the left graph of Fig. 5 that just the benefit of the trian-
TRA*(F=1)
100 150 and the added benefit of the abstraction makes TRA*’s first
80
60
100 solution even faster. We also see how many node expan-
40 50
sions are saved using these methods — in the right graph,
20 TA*’s node expansions are barely visible, with TRA*’s (not
0 0
0 100 200 300 400 500 0 100 200 300 400 500 shown) being significantly less than that.
A* path length A* path length For the environments used, the preprocessing took
roughly 20 µs per triangle, with almost half the time being
Figure 5: Median speedup and 90th node expansion perc. spent on each the triangulation and abstraction portions and
an almost negligible amount on point location sectors.
TA* Path Length Ratio (75. perc.) TA* Path Length Ratio (95. perc.)
1.08 1.1 Conclusion and Future Work
1.06 1.08 A*
1.04
A*
1.06 F=1 In this paper we have shown several benefits of
F=1
triangulation-based pathfinding including precisely repre-
Length Ratio
F=2 F=4
1.02 1.16
F=4
1 bound
F=8
1.12 bound ing a channel instead of a single path could be useful when
0.98 1.08 considering multiple units moving in a group. Also the ab-
1.04
0.96
1 straction information would be an excellent basis for terrain
0.94 0.96 analysis, since dead ends, corridors, intersections, and choke
0.92 0.92
0 100 200 300 400 500 -100 0 100 200 300 400 500 points are already identified. Finally, many techniques de-
A* path length A* path length signed for use with a grid representation have potential on a
triangulation with minor modification.
Figure 6: Path length ratio percentiles for TA* and TRA*
Acknowledgments
We thank Nathan Sturtevant for his assistance with this pa-
nitely within 4% of the optimal path length 75% of the time, per. Financial support was provided by NSERC and iCORE.
and within 6%, 95% of the time. As these graphs show, TA*
finds its shortest path most of the time right away, although References
less so on longer paths. As seen in the top-left graph, in 75%
of paths, the first solution found rarely differs from the last, Botea, A.; Müller, M.; and Schaeffer, J. 2004. Near optimal
only when looking at the 95th percentile graph do we see hierarchical path-finding. J. of Game Develop. 1(1):7–28.
that a need for more time to achieve this result. Hershberger, J., and Snoeyink, J. 1994. Computing min-
The bottom graphs show that TRA* requires higher F imum length paths of a given homotopy class. Computa-
values to avoid this departure from the final solution. This tional Geometry Theory and Application 4:63–98.
is because TRA*’s slightly less precise g-values sometimes Holte, R.; Perez, M.; Zimmer, R.; and MacDonald, A.
cause it to take several times longer to find its final solution 1996. Hierarchical A*: Searching abstraction hierarchies
than its first. However, because TRA* finds its first solution efficiently. In AAAI/IAAI Vol. 1, 530–535.
so much faster than TA*, it can often reach its final solution Kallmann, M.; Bieri, H.; and Thalmann, D. 2003. Fully
before TA* finds its first. We also see that even when the dynamic constrained delaunay triangulations. In Geomet-
path degenerates from TA*(10), the path length is usually ric Modelling for Scientific Visualization. Springer-Verlag.
still less than the optimal path when constrained to the grid. 241–257.
The fact that the time to find the optimal solution, or one Kallmann, M. 2005. Path planning in triangulations. In
near it, increases with the A* path distance of the path can Proceedings of the IJCAI Workshop on Reasoning, Repre-
be used to our advantage. The length of the first path found sentation, and Learning in Computer Games, 49–54.
can be used to determine for how much longer the algorithm
Sturtevant, N., and Buro, M. 2005. Partial pathfinding
should be run in order to be likely to find a path within a
using map abstraction and refinement. AAAI 1392–1397.
certain amount of optimal.
947