A Fast Triangle-Triangle Intersection Test
Tomas Möller
Abstract
This paper presents a method, along with some optimizations, for comput-
ing whether or not two triangles intersect. The code, which is shown to be fast,
can be used in, for example, collision detection algorithms.
1 Introduction
Most collision detection algorithms, such as OBBTree [Gottschalk96], sphere hier-
archies [Hubbard96] and BV-trees [Klosowski97], try to minimize the number of
primitive-primitive intersections that have to be computed. Still, a fast and reliable
method for computing the primitive-primitive intersection is desired. Since render-
ing hardware is often targeted for triangles, the primitives in collision detection al-
gorithms are often triangles as well. This paper describes a method for determining
if two triangles intersect.
2 Intersection Test Method
Let us denote the two triangles T1 and T2 ; the vertices of T1 and T2 by V01 , V11 , V21 ,
and V02 , V12 , V22 respectively; and the planes in which the triangles lie 1 and 2 .
First, the plane equation 2 : N2 X + d2 = 0 (where X is any point on the
plane) is computed:
N2 = (V12 V02 ) (V22 V02 )
d2 = N2 V02 : (1)
Then the signed distances from the vertices of T1 to 2 (multiplied by a constant
N2 N2 ) are computed by simply inserting the vertices into the plane equation:
1
dV 1 = N2 Vi + d2 ; i = 0; 1; 2:
i
(2)
Now, if all dV 1
i
6= 0, i = 0; 1 and 2 (that is, no point is on the plane) and all have
the same sign, then T1 lies on one side of 2 and the overlap is rejected. The same
is done for T2 and 1 . These two early rejection tests avoid a lot of computations
for some triangle pairs. Indeed, for a pair to pass this test there must be some line of
direction N1 N2 that meets both.
1
If all dV 1 = 0, i = 0; 1 and 2, then the triangles are co-planar, and this case
i
is handled separately and discussed later. If not, the intersection of 1 and 2 is a
line, L = O + tD , where D = N1 N2 is the direction of the line and O is some
point on it. Note that due to our previous calculations and rejections, both triangles
are guaranteed to intersect L. These intersections form intervals on L, and if these
intervals overlap, the triangles overlap as well. A similar interval test is used in a
different context by Laidlaw et al. [Laidlaw86]. Two situations that can occur are
depicted in figure 1.
L L
Figure 1: Triangles and the planes in which they lie. Intersection intervals are
marked gray in both figures. Left: the intervals along L overlap as well as the
triangles. Right: no intersection, the intervals do not overlap.
Now, assume that we want to compute a scalar interval (on L) that represents the
intersection between T1 and L, and that, for example, V01 and V21 lie on the same
side of 2 and that V11 lies on the other side (if not, you have already rejected it). To
find scalar values that represent the intersection between the edges V01 V11 and V11 V21
and L, the vertices are first projected onto L:
i
pV 1 = D (Vi
1
O ): (3)
The geometrical situation is shown in figure 2. Then we want to compute a line
parameter value, t1 , for B = V01 V11 \ L = O + t1 D . Letting Ki1 denote the
projection of Vi1 onto 2 , we see that 4V01 BK01 and 4V11 BK11 are similar, so
dV 1
0
t1 = p 1 + ( p 1 p 1) : (4)
V0 V1 V0 d dV 1
V01 1
Similar calculations are done to compute t2 , and an interval for T2 is computed as
well. If these intervals overlap, the triangles intersect.
If the triangles are co-planar, they are projected onto the axis-aligned plane
where the areas of the triangles are maximized. Then a simple two-dimensional
triangle-triangle overlap test is performed. First, test all closed edges of T1 for inter-
section with the edges of T2 . If any intersection is found, then the triangles intersect.
2
V01 V21
L
dV1
pV =D.(V11−O)
1
0 1
K01
t2
K11
pV =D.(V01−O)
2
0
1 t1 (B) dV1
1
V11
1
Figure 2: The geometrical situation: Vi1 are the vertices of T1 , 1 & 2 are the
planes in which T1 and T2 lie; dV 1 are the signed distances from Vi1 to 2 ; Ki1 are
i
the projections of Vi1 onto 2 ; and pV 1 are the projections of Vi1 onto L, which is
i
the line of intersection.
Otherwise, we must test if T1 is totally contained in T2 or vice versa. This can be
done by performing a point-in-triangle test [Haines94] for one vertex of T1 against
T2 and vice versa.
2.1 Optimizations
Since the intervals can be translated without altering the result of the interval overlap
test, equation (3) can be simplified into:
1
pV 1 = D Vi ; i = 0; 1; 2:
i
(5)
Therefore O does not need to be computed.
Also, the result of the overlap test does not change if we project L onto the
coordinate axis with which it is most closely aligned. Therefore equation (5) can be
simplified further:
8 V 1;
< 1 ix if jD j = max(jD j; jD j; jD j)
x x y z
p 1
Vi =
: VV 1 ;;iy if jD j = max(jD j; jD j; jD j)
y x y z ; i = 0; 1; 2: (6)
iz if jD j = max(jD j; jD j; jD j)
z x y z
Here, V01x means the x-component of V01 and so on. The same principle was used by
Mirtich [Mirtich96] in order to get a numerically stable simplification of an integral
over a polygon’s area.
3
3 Implementation & Performance
To summarize, the steps of the algorithm are as follows (complete C code is available
at http://www.acm.org/jgt/papers/Moller97/):
1. Compute plane equation of triangle 2.
2. Reject as trivial if all points of triangle 1 are on same side.
3. Compute plane equation of triangle 1.
4. Reject as trivial if all points of triangle 2 are on same side.
5. Compute intersection line and project onto largest axis.
6. Compute the intervals for each triangle.
7. Intersect the intervals.
Note that after step 2, there is enough information to immediately test whether the
triangles are co-planar, but because this is a rare occurrence, the test is deferred until
after several more frequently occuring rejection tests have been performed.
Robustness problems may arise when the triangles are nearly co-planar or when
an edge is near co-planar to the other triangle (especially when the edge is close to
an edge of the other triangle). To handle these cases in a reasonable way the source
code provides a constant EPSILON () which the user defines. As a result, if any
jdV k j < , they are reset so that dV k = 0. Geometrically, this means that if a point
i i
is “close enough” to the other triangle’s plane, it is considered as being on the plane.
The source code does not handle degenerate triangles (i.e., lines and points). If it
did, then those cases would have to be detected first and then handled as special
cases.
Performance was measured for several different scenarios in a collision detection
program [RAPID97]. Both our method and the method from ERIT [Held97] were
found to be faster than the brute-force method,1 which took between 1:3 and 1:6
times longer to execute. Our method was found to be slightly faster (in RAPID)
in most cases when compared to the author’s implementation of ERIT’s method.
We also tested the performance exactly like ERIT and found that our method was
approximately 7 percent faster on an SGI Impact. However, on the SGI O2, we
found that our method had a slight tendency to be faster for lower collision ratios
(i.e. the number of triangle-triangle collisions divided by the number of tests), and
the break even point was around 45 percent. Therefore, having worse bounding
volumes in a collision detection program implies that our method should perform
better than ERIT and vice versa.
Our method is also used in an in-house collision detection package for a com-
mercial VR-platform [Oxygen97].
1 Here, each closed edge of each triangle is tested for intersection with the other triangle and if, at any
time, an intersection occurs, then the triangles intersect. However, this method does not (easily) handle
cases where one or three edges are parallel to the plane of the other triangle.
4
Acknowledgements. A special thanks to Stefan Gottschalk (UNC) for many valuable
discussions on intersection tests. Constructive criticism and testing on an early version of this
work came from Jacob Ström, Per Carleberg, Pär Oscarsson, Patric Fredin and Ola Liljedahl.
Finally, thanks to both John F. Hughes and Ronen Barzel for helping me improve the contents
this paper.
References
[Gottschalk96] S. Gottschalk, M.C. Lin, D. Manocha, “OBBTree: A Hierarchi-
cal Structure for Rapid Interference Detection,” Computer Graph-
ics (SIGGRAPH’96 Proceedings), pp. 171–180, August, 1996.
[Haines94] Eric Haines, “Point in Polygon Strategies,” in Graphics Gems IV,
edited by Paul S. Heckbert, Academic Press Professional, Cam-
bridge, MA, pp. 24–46, 1994.
[Held97] Martin Held, “ERIT – A Collection of Efficient and Reliable Inter-
section Tests,” submitted to journal of graphics tools, 1997.
[Hubbard96] Philip M. Hubbard, “Approximating Polyhedra with Spheres for
Time-Critical Collision Detection,” ACM Transactions on Graph-
ics, vol. 15, no. 3, pp. 179–210, 1996.
[Klosowski97] James T. Klosowski, Martin Held, Joseph S.B. Mitchell, Henry
Sowizral, Karel Zikan, “Efficient Collision Detection Using Bound-
ing Volume Hierarchies of k-DOPs,” submitted for publication,
IEEE Transactions on Visualization and Computer Graphics, 1997.
[Laidlaw86] David H. Laidlaw, W. Benjamin Trumbore, John F. Hughes, “Con-
structive Sold Geometry for Polyhedral Objects,” Computer Graph-
ics (SIGGRAPH’86 Proceedings), vol. 20, no. 4, pp. 161–168,
1986.
[Mirtich96] Brian Mirtich, “Fast and Accurate Computation of Polyhedral Mass
Properties,” journal of graphics tools, vol. 1, no. 2, pp. 31–50, 1996.
[Oxygen97] “Oxygen Base User’s Guide 1.0”, Gothenburg, Sweden, Prosolvia
Clarus AB, 1997.
[RAPID97] Source code for collision detection by the Research Group
on Modeling, Physically-Based Simulation and Applica-
tions at University of North Chapel Hill, “RAPID – Robust
and Accurate Polygon Interference Detection,” available at
http://www.cs.unc.edu/˜geom/OBB/OBBT.html,
1997.