3d geometry tutorials - david eberly

1134
AABB of a Transformed AABB David Eberly Geometric Tools, LLC http://www.geometrictools.com/ Copyright c 1998-2008. All Rights Reserved. Created: September 24, 2005 Last Modified: February 12, 2008 Contents 1 Two Dimensions 2 1

Upload: markusmayer

Post on 24-Apr-2015

380 views

Category:

Documents


9 download

TRANSCRIPT

AABB of a Transformed AABBDavid EberlyGeometric Tools, LLChttp://www.geometrictools.com/Copyright c 1998-2008. All Rights Reserved.Created: September 24, 2005Last Modied: February 12, 2008Contents1 Two Dimensions 21This document describes how to compute the axis-aligned bounding box (AABB) of another AABB, whichis transformed by a rotation and translation (applied in that order). The transformed AABB is an orientedbounding box (OBB). The goal is to quickly compute the minimum and maximum values in each dimensionfor the OBB without having to transform all the vertices and search them for the extrema. The constructionapplies to general dimensions, but the 2D and 3D cases are provided rst for motivation.1 Two DimensionsAn AABB in two dimensions is the set of points (x, y), where xmin x xmax and ymin y ymax. Theminimum and maximum x and y values are, of course, known quantities. The point in the AABB is writtenin vector form asP = xyThe rotation is a matrix R and the translation is a vector T, sayR = cos sin sin cos , T = abwhere is the angle of rotation. The transformed AABB point isQ = RP+T = xcos y sin + axsin + y cos + bThe four vertices of the AABB areP0 = (xmin, ymin)P1 = (xmax, ymin)P2 = (xmin, ymax)P3 = (xmax, ymax)The four transformed vertices are those of an OBB and areQ0 = (xmin cos ymin sin + a, xmin sin + ymin cos + b)Q1 = (xmin cos ymax sin + a, xmin sin + ymax cos + b)Q2 = (xmax cos ymin sin + a, xmax sin + ymin cos + b)Q3 = (xmax cos ymax sin + a, xmax sin + ymax cos + b)A naive method for computing the extreme values in x and y is to search the components directly. Thepseudocode isvoid GetAABB (Point2 min, Point2 max, Matrix2 rot, Point2 trn,2Point2& newmin, Point2& newmax){Real cs = rot[0][0], sn = rot[1][0];Real x, y;newmin.x = newmax.x = min.x * cs - min.y * sn + trn.x;newmin.y = newmax.y = min.x * sn + min.y * cs + trn.y;x = min.x * cs - max.y * sn + trn.x;y = min.x * sn + max.y * cs + trn.y;if (x < newmin.x) { newmin.x = x; } else if (x > newmax.x) { newmax.x = x; }if (y < newmin.y) { newmin.y = y; } else if (y > newmax.y) { newmax.y = y; }x = max.x * cs - min.y * sn + trn.x;y = max.x * sn + min.y * cs + trn.y;if (x < newmin.x) { newmin.x = x; } else if (x > newmax.x) { newmax.x = x; }if (y < newmin.y) { newmin.y = y; } else if (y > newmax.y) { newmax.y = y; }x = max.x * cs - max.y * sn + trn.x;y = max.x * sn + max.y * cs + trn.y;if (x < newmin.x) { newmin.x = x; } else if (x > newmax.x) { newmax.x = x; }if (y < newmin.y) { newmin.y = y; } else if (y > newmax.y) { newmax.y = y; }}This algorithm requires 16 multiplications, 16 additions (or subtractions), and at least 6 comparisons but atmost 12 comparisons.Noticing that the extrema calculations are dependent on the signs of cs and sn, the following pseudocodehas fewer operations and comparisons.3void GetAABB (Point2 min, Point2 max, Matrix2 rot, Point2 trn,Point2& newmin, Point2& newmax){Real cs = rot[0][0], sn = rot[1][0];if (cs >= 0){if (sn >= 0){newmin.x = min.x * cs - max.y * sn + trn.x;newmin.y = min.x * sn + min.y * cs + trn.y;newmax.x = max.x * cs - min.y * sn + trn.x;newmax.y = max.x * sn + max.y * cs + trn.y;}else{newmin.x = min.x * cs - min.y * sn + trn.x;newmin.y = max.x * sn + min.y * cs + trn.y;newmax.x = max.x * cs - max.y * sn + trn.x;newmax.y = min.x * sn + max.y * cs + trn.y;}}else{if (sn >= 0){newmin.x = max.x * cs - max.y * sn + trn.x;newmin.y = min.x * sn + max.y * cs + trn.y;newmax.x = min.x * cs - min.y * sn + trn.x;newmax.y = max.x * sn + min.y * cs + trn.y;}else{newmin.x = max.x * cs - min.y * sn + trn.x;newmin.y = max.x * sn + max.y * cs + trn.y;newmax.x = min.x * cs - max.y * sn + trn.x;newmax.y = min.x * sn + min.y * cs + trn.y;}}}This code requires 8 multiplications, 8 additions, and 2 comparisons.4Approximating an Ellipse by Circular ArcsDavid EberlyGeometric Tools, LLChttp://www.geometrictools.com/Copyright c 1998-2008. All Rights Reserved.Created: January 13, 2002Last Modied: February 12, 2008Contents1 Algorithm 22 Implementation 211 AlgorithmThe ellipses to be approximated are axis-aligned and centered at the origin,x2a2 + y2b2 = 1 (1)where a = b. Only the portion of the ellipse in the rst quadrant will be approximated by circular arcs. Theapproximations in the other quadrants can be obtained by reections of those in the rst quadrant.The rst part of the process involves selecting a set of ellipse points Pi = (xi, yi), 0 i n, whereP0 = (a, 0), Pn = (0, b), and the points are counterclockwise ordered in the rst quadrant. The selection isbased on weighted averages of curvatures. Given a parameterized curve (x(t), y(t)), the curvature isK(t) = x

(t)y

(t) y

(t)x

(t)((x

(t))2+ (y

(t))2)3/2.The ellipse is parameterized by x(t) = a cos t and b sin t for t [0, 2). The derivatives are x

(t) = a sin t,y

(t) = b cos t, x

(t) = a cos t, and y

(t) = b sin t. The curvature is K(t) = ab/(a2sin2t + b2cos2t)3/2.As a function of (x, y), the curvature isK(x, y) = ab

bxa

2+ ayb

2

3/2, (2)where it is understood that (x, y) additionally satises equation (1). If the curvature K is specied, thecorresponding (x, y) is obtained by solving equations (1) and (2). Setting = (ab/K)2/3, the solution isx = a

a2b2a2

, y = b

b2a2b2

.Dene K0 = K(a, 0) = a/b2and Kn = K(0, b) = b/a2. The point Pi for 1 i n 1 is selected so thatthe curvature is Ki = (1 i/n)K0 + (i/n)Kn.The second part of the process involves selecting a circular arc with center Ci and radius ri, 0 i < n, whoseend points are Pi and Pi+1. In order to obtain a vertical tangent at (a, 0), the circle (C0, r0) is chosen thatcontains P0, P1, and (x1, y1) where the last point is the reection of P1 through the x-axis. Similarly, inorder to obtain a horizontal tangent at (0, b), the circle (Cn1, rn1) is chosen that contains Pn1, Pn, and(xn1, yn1) where the last point is the reection of Pn1 through the y-axis. The other circles (Ci, ri)for 1 i n 2 are chosen to contain Pi1, Pi, and Pi+1.2 ImplementationThe source code for the approximation is in les Wm4ApprEllipseByArcs2.h and Wm4ApprEllipseByArcs2.cpp.A test program is shown next.#include "Wm4ApprEllipseByArcs2.h"#include "Wm4Images.h"2#include "Wm4Matrix2.h"#include "Wm4RasterDrawing.h"using namespace Wm4;static int gs_iB0 = 256, gs_iB1 = 256;static int gs_iB0M = gs_iB0-1, gs_iB1M = gs_iB1-1;static ImageRGB82D gs_kImage(gs_iB0,gs_iB0);static int gs_iColor = 0;//----------------------------------------------------------------------------static void SetPixel (int iX, int iY){if (0 R0 +R1for two OBBs in the direction of motion.82.3 Finding an Intersection of OBBsTesting for intersection is a boolean operation. The algorithm returns true if there is an intersection, falseif not. No information is provided about where an intersection occurs (there may be many such points).In the case of two stationary objects that are intersection, the region of intersection can be computed withgreat pain. This is the realm of computation solid geometry. For a dynamic system, the more interesting caseis to have two moving objects that are initially not intersecting, but then do intersect during the specied timeinterval. Of interest to an application is the rst time of intersection and a point (or points) of intersectionat that time. The following construction provides a point of intersection. In the cases of vertex-to-vertex,vertex-to-edge, vertex-to-face, or edge-to-edge (transversely), the intersection point (at rst time) is unique.In the other cases of edge-to-face or face-to-face, the intersection is not unique, but the construction providesone of the intersection points.2.3.1 Finding the First Time of IntersectionGiven that the two objects do not intersect at time t = 0, but do intersect at some later time, a simplemodication of the algorithm for testing for an intersection provides the rst time of intersection. The rsttime is computed as the maximum time T > 0 for which there is at least one separating axis for any t [0, T),but for which no separating axis exists at time T. The idea is to test each potential separating axis and keeptrack of the time at which the intervals of projection intersect for the rst time. The largest such time is therst time at which the OBBs intersect. Also, it is important to keep track of which side of [R0, R0] theother interval intersects. Finally, knowing the separating axis associated with the maximum time T allowsus to reconstruct a point of intersection.2.3.2 Finding a Point of IntersectionIf T is the rst time of intersection, the problem is now to nd a point P in the intersection of the two OBBsat that time. We need to solve2

i=0xiAi = D+2

j=0yjBj (3)where D = (C1 + TV1) (C0 + TV0), and for xi with |xi| ai, i = 0, 1, 2, and for yj with |yj| bj,j = 0, 1, 2.Last Separating Axis Ai. If the separating axis at time T is Ai, then the intersection must occur on oneof the two faces perpendicular to Ai. Dotting equation (3) with Ai yieldsxi = Ai D+2

j=0cijyj.If Ai D > 0, then Ai D = R0 + R1 since the two intervals intersect at the right endpoint of [R0, R0].If Ai D < 0, then Ai D = (R0 + R1) since the two intervals intersect at the left endpoint of [R0, R0].9Thus, Ai D = (R0 +R1) where || = 1 andxi = (R0 +R1) +

2j=0cijyj= (ai +

2j=0bj|cij|) +

2j=0cijyj0 = 2j=0|cij|(bj +Sign(cij)yj) + (ai xi).(4)Since |yj| bj and |Sign(cij)| 1, it must be that bj +Sign(cij)yj 0. Similarly, ai xi 0 in whichcase xi = ai. If cij = 0, then yj = Sign(cij)bj is required to make the sum in equation (4) zero.If any cij = 0, then the sum in equation (4) places no restriction on yj. For example, this happens when theintersection is edge-to-face or face-to-face. Instead take the dot product of equation (3) with Bj to obtainyj = Bj D+2

k=0xkckj.Using |xk| ak, we havemin(yj) = Bj D2

k=0|ckj|ak yj Bj D+2

k=0|ckj|ak = max(yj).Additionally we know |yj| bj. We must choose a value yj [min(yj), max(yj)] [bj, bj]. Since we knowan intersection must occur, min(yj) bj and bj max(yj). If bj max(yj), then yj = bj is an intersectionpoint. If bj min(yj), then yj = bj is an intersection point. Otherwise, we may choose yj = min(yj) asan intersection point.Last Separating Axis Bi. If the separating axis at time T is Bi, then the intersection must occur on oneof the two faces perpendicular to Bi. Dotting equation (3) with Bi yields2

j=0cjixj = Bi D+yi.As in the last section it can be shown that Bi D = (R0 +R1) where || = 1. Moreover,

2j=0cjixj = (R0 +R1) +yi= (

2j=0aj|cji| +bi) +yi0 = 2j=0|cji|(aj Sign(cji)xj) + (b0 +y0).(5)Since |xj| aj and |Sign(cji)| 1, it must be that aj Sign(cji)xj 0. Similarly, bi +yi 0 in whichcase yi = bi. If cji = 0, then xj = Sign(cji)aj is required to make the sum in equation (5) zero.If any cji = 0, then the sum in the displayed equation places no restriction on xi. For example, this happenswhen the intersection is edge-to-face or face-to-face. Instead take the dot product of equation (3) with Ajto obtainxj = Aj D+2

k=0ykcjk.10Using |yk| bk, we havemin(xj) = Aj D2

k=0|cjk|bk xj Aj D+2

k=0|cjk|bk = max(xj).Additionally we know |xj| aj. We must choose a value xj [min(xj), max(xj)] [aj, aj]. Since weknow an intersection must occur, min(xj) aj and aj max(xj). If aj max(xj), then xj = aj is anintersection point. If aj min(xj), then xj = aj is an intersection point. Otherwise, we may choosexj = min(xj) as an intersection point.Last Separating Axis Ai Bj. Let (i0, i1, i2) and (j0, j1, j2) be permutations of (0, 1, 2) in the set{(0, 1, 2), (1, 0, 2), (2, 1, 0)}. Dot equation (3) with Ai0 Bj0 to obtain(Ai1 Ai0 Bj0)xi1 + (Ai2 Ai0 Bj0)xi2 = Ai0 Bj0 D+ (Bj1 Ai0 Bj0)yj1 + (Bj2 Ai0 Bj0)yj2Sign(i1, i0)ci2j0xi1 + Sign(i2, i0)ci1j0xi1 = (|ci2j0|ai1 +|ci1j0|ai2 +|ci0j2|bj1 +|ci0j1|bj2)+Sign(j0, j1)ci0j2yj1 + Sign(j0, j2)ci0j1yj2.where || = 1. Grouping terms and factoring yields0 = |ci2j0|(ai1 Sign(i1, i0)Sign(ci2j0)xi1) +|ci1j0|(ai2 Sign(i1, i0)Sign(ci1j0)xi2)+|ci0j2|(bj1 Sign(j1, j0)Sign(ci0j2)yj1) +|ci0j1|(bj2 Sign(j2, j0)Sign(ci0j1)yj2)As in the previous section, the quantities multiplying the |cij| terms must be zero when the cij term is notzero.The rst case to consider is ci2j0 = 0, ci1j0 = 0, ci0j2 = 0, and ci0j1 = 0. Thenxi1 = Sign(i1, i0)Sign(ci2j0)ai1xi2 = Sign(i2, i0)Sign(ci1j0)ai2yj1 = Sign(j1, j0)Sign(ci0j2)bj1yj2 = Sign(j2, j0)Sign(ci0j1)bj2.To solve for xi0 and yj0, dot equation (3) with Ai0 and Bj0 to obtainxi0 = Ai0 D+ci0j0yj0 +ci0j1yj1 +ci0j2yj2,ci0j0xi0 +ci1j0xi1 +ci2j0xi2 = Bj0 D+yj0.Replacing each equation in the other yieldsxi0 = 11c2i0j0[Ai0 D+ci0j0 (Bj0 D+ci1j0xi1 +ci2j0xi2) +ci0j1yj1 +ci0j2yj2]yj0 = 11c2i0j0[Bj0 D+ci0j0 (Ai0 D+ci0j1yj1 +ci0j2yj2) +ci1j0xi1 +ci2j0xi2]The denominator of the fraction is not zero since 1 c2i0j0 = c2i1j0 +c2i2j0 = 0 since ci1j0 = 0 and ci2j0 = 0.11Geometrically, these four cij numbers must be zero since this case represents either (1) an edge-to-edgecollision and the intersection point must be unique or (2) an edge-to-edge collision where the edges areperfectly aligned. In the latter case, a face axis should separate the two OBBs. Just in case the face axisseparation does not happen due to numerical round-o errors, the code has cases to handle whenever any ofthe cij = 0. The handlers are similar to what was discussed earlier. The intersection equation is dotted withthe appropriate vector to solve explicitly for the to-be-determined variable (a xi or a yj term). Inequalititesare obtained for that variable and the minimum and maximum values are used as before to nd a point inthe intersection of two intervals for that variable.The coecients needed to produce the unique points of intersection are summarized in the following table.L coecientsAi yj = Sign(cij)bj, j = 0, 1, 2Bj xi = +Sign(cij)ai, i = 0, 1, 2A0 B0 x1 = Sign(c20)a1, x2 = +Sign(c10)a2, y1 = Sign(c02)b1, y2 = +Sign(c01)b2,x0 = 11c200(A0 D+c00(B0 D+c10x1 +c20x2) +c01y1 +c02y2)A0 B1 x1 = Sign(c21)a1, x2 = +Sign(c11)a2, y0 = +Sign(c02)b0, y2 = Sign(c00)b2,x0 = 11c201(A0 D+c01(B1 D+c11x1 +c21x2) +c00y0 +c02y2)A0 B2 x1 = Sign(c22)a1, x2 = +Sign(c12)a2, y0 = Sign(c01)b0, y1 = +Sign(c00)b1,x0 = 11c202(A0 D+c02(B2 D+c12x1 +c22x2) +c00y0 +c01y1)A1 B0 x0 = +Sign(c20)a0, x2 = Sign(c00)a2, y1 = Sign(c12)b1, y2 = +Sign(c11)b2,x1 = 11c210(A1 D+c10(B0 D+c00x0 +c20x2) +c11y1 +c12y2)A1 B1 x0 = +Sign(c21)a0, x2 = Sign(c01)a2, y0 = +Sign(c12)b0, y2 = Sign(c10)b2,x1 = 11c211(A1 D+c11(B1 D+c01x0 +c21x2) +c10y0 +c12y2)A1 B2 x0 = +Sign(c22)a0, x2 = Sign(c02)a2, y0 = Sign(c11)b0, y1 = +Sign(c10)b1,x1 = 11c212(A1 D+c12(B2 D+c02x0 +c22x2) +c10y0 +c11y1)A2 B0 x0 = Sign(c10)a0, x1 = +Sign(c00)a1, y1 = Sign(c22)b1, y2 = +Sign(c21)b2,x2 = 11c220(A2 D+c20(B0 D+c00x0 +c10x1) +c21y1 +c22y2)A2 B1 x0 = Sign(c11)a0, x1 = +Sign(c01)a1, y0 = +Sign(c22)b0, y2 = Sign(c20)b2,x2 = 11c221(A2 D+c21(B1 D+c01x0 +c11x1) +c20y0 +c22y2)A2 B2 x0 = Sign(c12)a0, x1 = +Sign(c02)a1, y0 = Sign(c21)b0, y1 = +Sign(c20)b1,x2 = 11c222(A2 D+c22(B2 D+c02x0 +c12x1) +c20y0 +c21y1)Table 2. Coecients for unique points of OBB-OBB intersection.123 Triangle and Oriented Bounding BoxTriangles are represented in this framework as a collection of three vertices Ui, i = 0, 1, 2. The edges of thetriangle are E0 = U1 U0, E1 = U2 U0, and E2 = E1 E0. A normal for the triangle is N = E0 E1and is not necessarily unit length. The triangle and its interior are given by{U0 +sE0 +tE1 : 0 s 1, 0 t 1, s +t 1} .3.1 Separation of Triangle and OBBLet the OBB have center C, axes A0, A1, A2, and extents a0, a1, a2. Let the triangle have vertices U0, U1,U2; edges E0 = U1 U0, E1 = U2 U0, E2 = E1 E0; and normal N = E0 E1. Dene D = U0 C.The potential separating axes are of the form C+sL where L is one of N, Ai, or AiEj for i = 0, 1, 2 andj = 0, 1, 2.For the OBB, the projection distances of the vertices relative to the line origin C areProjDist_C+2

i=0iaiAi_=2

i=0iaiL AiL Land the minimum length interval containing all eight projection distances has center 0 and radiusr =

2i=0aiSign(L Ai)L AiL L .The projection distances of the triangles vertices relative to the line origin areProjDist(Uk C) = L (Uk C)L Lfor k = 0, 1, 2. The projection of the triangle does not have a natural center or radius as does the OBB.Nonintersection now amounts to showing that the minimal interval containing the three projected trianglevertices is separated from the projected OBB interval.Dene R to be r without the division by squared length of L,R =2

i=0aiSign(L Ai)L Ai. (6)The projection distances without the division by squared length of L arep0 = L (U0 C) = L Dp1 = L (U1 C) = L (D+E0) = p0 +L E0p2 = L (U2 C) = L (D+E1) = p0 +L E1.(7)13Equations (6) and (7) lead to the entries in the table below.L p0 p1 p2 RN N D p0 p0 a0|N A0| +a1|N A1| +a2|N A2|A0 A0 D p0 +A0 E0 p0 +A0 E1 a0A1 A1 D p0 +A1 E0 p0 +A1 E1 a1A2 A2 D p0 +A2 E0 p0 +A2 E1 a2A0 E0 A0 E0 D p0 p0 +A0 N a1|A2 E0| +a2|A1 E0|A0 E1 A0 E1 D p0 A0 N p0 a1|A2 E1| +a2|A1 E1|A0 E2 A0 E2 D p0 A0 N p0 A0 N a1|A2 E2| +a2|A1 E2|A1 E0 A1 E0 D p0 p0 +A1 N a0|A2 E0| +a2|A0 E0|A1 E1 A1 E1 D p0 A1 N p0 a0|A2 E1| +a2|A0 E1|A1 E2 A1 E2 D p0 A1 N p0 A1 N a0|A2 E2| +a2|A0 E2|A2 E0 A2 E0 D p0 p0 +A2 N a0|A1 E0| +a1|A0 E0|A2 E1 A2 E1 D p0 A2 N p0 a0|A1 E1| +a1|A0 E1|A2 E2 A2 E2 D p0 A2 N p0 A2 N a0|A1 E2| +a1|A0 E2|Table 3. Values for R, p0, p1, and p2 for the nonintersection testmin(p0, p1, p2) > R or max(p0, p1, p2) < R for triangle andOBB.For axis direction N, the projected triangle vertices are identical, so the nonintersection test amounts toshowing N D is not in the interval [R, R]. For axis directions Ai, the projected triangle vertices may allbe distinct. For axis directions Ai Ej, at most two of the projected vertices are distinct. If the triangleinterval is [min(p0, p1, p2), max(p0, p1, p2)] and the OBB interval is [R, R], then the triangle and OBB donot intersect whenever min(p0, p1, p2) > R or max(p0, p1, p2) < R.3.2 Testing for Intersection of Triangle and OBB3.2.1 StationaryTesting for intersection amounts to processing each axis of the 13 potential separating axes. If a separatingaxis is found, the remaining ones are not processed. Any quantities that are needed multiple times arecalculated only once and only when needed.Axis N. The nonintersection test is |N D| > R. Pseudocode for testing if p is not in [R, R] isif ( |p| > R )return no_intersection;14Axes Ak. The p-values are p, p +d0, and p +d1. The nonintersection test is: min(p, p +d0, p +d1) > R ormax(p, p +d0, p +d1) < R. Pseudocode isif ( p > R ){if ( d0 >= 0 ){if ( d1 >= 0 )return no_intersection; // pmin = p > Rif ( p+d1 > R )return no_intersection; // pmin = p+d1 > R}else if ( d1 R )return no_intersection; // pmin = p+d1 > R}else{if ( p+d0 > R )return no_intersection; // pmin = p+d0 > R}}else if ( p < -R ){if ( d0 R or max(p +d) < R). Pseudocode isif ( p > R )15{if ( d >= 0 )return no_intersection; // pmin = p > Rif ( p+d > R )return no_intersection; // pmin = p+d > R}else if ( p < -R ){if ( d R for allt [0, T] or max(p)(t) < R for all t [0, T]. Because the linear velocity is constant, it is enough to shownonintersection by verifying that min{min(p)(0), min(p)(T)} > R or max{max(p)(0), max(p)(T)} < R.Axis N. The nonintersection test amounts to showing p +tw is not in [R, R] for t [0, T] (p = N D andw = N W). Moreover, the point p + tw must not pass through [R, R] during the given time period, sothe algorithm keeps track of which side of [R, R] the point starts on. Pseudocode isif ( p > R ){if ( p+T*w > R )return no_intersection;}else if ( p < -R ){if ( p+T*w < -R )return no_intersection;}Axes Ak. The problem is to make sure that the minimum interval containing {p+Tw, p+d0+Tw, p+d1+Tw}does not intersect [R, R]. When w = 0 (the static case), the pseudocode for nonintersection was givenearlier. For the case when w = 0, each case where a no intersection is returned when t = 0 must be renedto make sure the projected triangle interval remains on the same side of [R, R] at t = T. The pseudocodeis16if ( p > R ){if ( d0 >= 0 ){if ( d1 >= 0 ){min = p;if ( min+T*w > R )return no_intersection;}else{min = p+d1;if ( min > R && min+T*w > R )return no_intersection;}}else if ( d1 R && min+T*w > R )return no_intersection;}else{min = p+d0;if ( min > R && min+T*w > R )return no_intersection;}}}else if ( p < -R ){if ( d0 R ){if ( d >= 0 ){min = p;if ( min+T*w > R )return no_intersection;}else{min = p+d;if ( min > R && min+T*w > R )return no_intersection;}}else if ( p < -R ){if ( d 0 for which there is at least one separating axis for any t [0, T),but for which no separating axis exists at time T. The idea is to test each potential separating axis andkeep track of the time at which the intervals of projection intersect for the rst time. The largest such timeis the rst time at which the triangle and OBB intersect. Also, it is important to keep track of which sideof [R, R] the other interval intersects. Finally, knowing the separating axis associated with the maximumtime T allows us to reconstruct a point of intersection.3.3.2 Finding a Point of IntersectionIf T is the rst time of intersection, the problem is now to nd a point P in the intersection of the triangleand OBB at that time. We need to solve2

i=0xiAi = D+y0E0 +y1E1 (8)where D = (C + TV1) (U0 + TV0), and for xi with |xi| ai, i = 0, 1, 2, and for yj with 0 y0 1,0 y1 1, and y0 +y1 1.Equation (8) can be solved for each variable individually. The solutions arexi = Ai D+ (Ai E0)y0 + (Ai E1)y1yj = 12j|N|2_N DE1j +

2i=0xiN Ai E1j_ (9)for i = 0, 1, 2 and j = 0, 1. The equations dene the left-hand side as a linear function of the variables inthe right-hand side. The x extreme values occur at the vertices of the triangular domain of the function:19(0, 0), (1, 0), and (0, 1). The extreme values of the equations dene intervals bounding each variable, xi [min(xi), max(xi)]. The interval end points aremin(xi) = Ai D+ min(0, Ai E0, Ai E1),max(xi) = Ai D+ max(0, Ai E0, Ai E1).(10)The y extreme values occur at the vertices of the rectoidal domain of the function (all |xi| = ai). The extremevalues of the equations dene intervals bounding each variable, yj [min(yj), max(yj)]. The interval endpoints aremin(yj) = (2j1)NDE1j

2i=0 ai|NAiE1j||N|2 ,max(yj) = (2j1)NDE1j+

2i=0 ai|NAiE1j||N|2 .(11)In the following constructions of the rst point of intersection, if any of the variables is not uniquely con-strained by the derived equations, then such a variable can be selected from the intervals [min(xi), max(xi)]or [min(yj), max(yj)] and subject to the other domain constraints for that variable.The following are a few useful functions in the constructions. Dene , , , and by(k) =___0, k = 01, k = 11, k = 2___, (k) =___1, k = 00, k = 11, k = 2___,(k) =___1, k = 00, k = 11, k = 2___, (k) =___1, k = 01, k = 11, k = 2___.Some useful identities are 2 1, 2 1, , and .Last Separating Axis N. If the separating axis at time T is N, then the intersection must occur on thetriangle itself. Dotting equation (8) with N yields2

i=0xiN Ai = N D.Note that N D = R for || = 1. Thus,

2i=0xiN Ai = R

2i=0xiN Ai =

2i=0ai|N Ai|0 = 2i=0|N Ai|(ai Sign(N Ai)xi).(12)Since |xi| ai and |Sign(N Ai)| 1, it must be that ai Sign(N Ai)xi 0. If N Ai = 0, thenxi = Sign(N Ai)ai is required to make the sum in equation (12) zero. If any N Ai = 0, then the sum inequation (12) places no restriction on xi. For example, this happens when the intersection is edge-to-triangleor face-to-triangle. Any xi [min(xi), max(xi) [ai, ai] can be selected for a point of intersection.20Last Separating Axis Ai. If the separating axis at time T is Ai, then the intersection must occur on oneof the two OBB faces perpendicular to Ai and R = ai. The formula for xi from equation (9) has three casesto be considered.The rst case is p0 = minj(pj) = ai in which case = 1 or p0 = maxj(pj) = ai in which case = 1.Then Ai E0 0, Ai E1 0, and xi = ai +y0Ai E0 +y1Ai E1. The intersection occurs on a face ofthe OBB perpendicular to Ai, so it must be that xi = ai and0 = (Ai E0)y0 + (Ai E1)y1. (13)If Ai E0 = 0 and Ai E1 = 0, then y0 = 0 and y1 = 0 are required. If Ai E0 = 0 and Ai E1 = 0, equation(13) requires y1 = 0 but does not constrain y0. In this case y0 [min(y0), max(y0)] [0, 1] where min(y0)and max(y0) are dened in equation (11). If Ai E0 = 0 and Ai E1 = 0, equation (13) requires y0 = 0 butdoes not constrain y1. In this case y1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are denedin equation (11). If Ai E0 = 0 and Ai E1 = 0, equation (13) constrains neither y0 nor y1. The OBBintersects the triangle face-to-face, a case handled by the separating axis test for N.The second case is p1 = minj(pj) = ai in which case = 1 or p1 = maxj(pj) = ai in which case = 1.Then Ai E0 0, (Ai E1Ai E0) 0, and xi = aiAi E0 +y0Ai E0 +y1Ai E1. Once again,xi = ai and0 = (Ai E0)(1 y0 y1) + [(Ai E1 Ai E0)]y1. (14)If Ai E0 = 0 and Ai E1 = Ai E0, then 1 y0 y1 = 0 and y1 = 0 are required. Therefore, y0 = 1and y1 = 0. If Ai E0 = 0 and Ai E1 = Ai E0, equation (14) requires y1 = 0 but does not constrainy0. In this case y0 [min(y0), max(y0)] [0, 1] where min(y0) and max(y0) are dened in equation (11). IfAi E0 = 0 and Ai E1 = Ai E0, equation (14) requires y0 +y1 = 1 but does not constrain y1. In this casey1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are dened in equation (11). Given a choice ofy1, then y0 = 1 y1. If Ai E0 = Ai E1 = 0, then neither y0 nor y1 is constrained. In this event, N andAi are parallel, a case handled by the separating axis test for N.The third case is p2 = minj(pj) = ai in which case = 1 or p2 = maxj(pj) = ai in which case = 1.Then Ai E1 0, (Ai E0Ai E1) 0, and xi = aiAi E1 +y0Ai E0 +y1Ai E1. Once again,xi = ai and0 = [(Ai E0 Ai E1)]y0 + (Ai E1)(1 y0 y1). (15)If Ai E1 = 0 and Ai E0 = Ai E1, then 1 y0 y1 = 0 and y0 = 0 are required. Therefore, y0 = 0 andy1 = 1. If Ai E1 = 0 and Ai E0 = Ai E1, equation (15) requires y0 = 0 but does not constrain y1. In thiscase y1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are dened in equation (11). Given a choiceof y1, then y0 = 1 y1. If Ai E1 = 0 and Ai E0 = Ai E1, equation (15) requires 1 y0 y1 = 0 butdoes not constrain y0. In this case y0 [min(y0), max(y0)] [0, 1] where min(y0) and max(y0) are denedin equation (11). Given a choice of y0, then y1 = 1 y0. If Ai E1 = Ai E0 = 0, then neither y0 nor y1 isconstrained. In this event, N and Ai are parallel, a case handled by the separating axis test for N.Last Separating Axis Ai Ej. Let (i0, i1, i2) and (j0, j1, j2) be permutations of (0, 1, 2) in the set{(0, 1, 2), (1, 0, 2), (2, 1, 0)}. Dot equation (8) with Ai0 Ej0 to obtain(Ai1 Ai0 Ej0)xi1 + (Ai2 Ai0 Ej0)xi2 = Ai0 Ej0 D+ (E0 Ai0 Ej0)y0 + (E1 Ai0 Ej0)y1.Using various identities, the equation reduces to(Sign(i1, i0)Ai2 Ej0)xi1 + (Sign(i2, i0)Ai1 Ej0)xi2 = p0 (j0)N Ai0y0 (j0)N Ai0y1. (16)21where p0 = Ai0 Ej0 D from Table 3. The projection of the triangles vertices leads to (possibly) twodistinct p values, {p0, p0(j0)N Ai0}. There are two cases to consider depending on which of the p valuesare minima or maxima. In all cases,R = ai1|Ai2 Ej0| +ai2|Ai1 Ej0|.The rst case is p0 = mink(pk) = R in which case = 1 or p0 = maxk(pk) = R in which case = 1.Then p0 = R and (j0)Ai0 N 0. Equation (16) is equivalent to0 = |Ai2 Ej0|(ai1 Sign(i1, i0)Sign(Ai2 Ej0)xi1)+|Ai1 Ej0|(ai2 Sign(i2, i0)Sign(Ai1 Ej0)xi2)+((j0)Ai0 N)((j0)y0 +(j0)y1).If Ai2 Ej0 = 0, then xi1 = Sign(i1, i0)Sign(Ai2 Ej0)ai1. If Ai1 Ej0 = 0, then xi2 = Sign(i2, i0)Sign(Ai1Ej0)ai2. If Ai0 N = 0, then (j0)y0 + (j0)y1 = 0. These provide three equations in ve unknowns. Twoadditional equations to form an invertible system can be selected from equations (9).The second case is p0(j0)NAi0 = mink(pk) = R in which case = 1 or p0(j0)NAi0 = maxk(pk) = Rin which case = 1. Then p0 = R +(j0)N Ai0 and (j0)Ai0 N 0. Equation (16) is equivalent to0 = |Ai2 Ej0|(ai1 Sign(i1, i0)Sign(Ai2 Ej0)xi1)+|Ai1 Ej0|(ai2 Sign(i2, i0)Sign(Ai1 Ej0)xi2)+((j0)Ai0 N)(1 (j0)y0 (j0)y1).If Ai2 Ej0 = 0, then xi1 = Sign(i1, i0)Sign(Ai2 Ej0)ai1. If Ai1 Ej0 = 0, then xi2 = Sign(i2, i0)Sign(Ai1Ej0)ai2. If Ai0 N = 0, then (j0)y0 + (j0)y1 = 1. These provide three equations in ve unknowns. Twoadditional equations to form an invertible system can be selected from equations (9).If any of the xi or yj are not constrained because their coecients are zero, a similar construction can beused as before where intervals are obtained on each of the variables and the intersection of those intervalswith their natural restrictions produce a point of intersection.The coecients needed to produce the unique points of intersection are summarized in the following tables.L coecientsN xi = +Sign(N Ai)ai, i = 0, 1, 2Ai y0 = 0, y1 = 0, p0 = mink(pk)y0 = 1, y1 = 0, p1 = mink(pk)y0 = 0, y1 = 1, p2 = mink(pk)Table 4a. Coecients for unique points of OBB-Triangle intersection for N and Ai.22L coecientsA0 E0 x1 = Sign(A2 E0)a1, x2 = +Sign(A1 E0)a2,y1 =___0, p0 = mink(pk)1, (p0 +N A0) = mink(pk)___x0 = NDE0|N|2y1NA1E0x1NA2E0x2NA0E0A0 E1 x1 = Sign(A2 E1)a1, x2 = +Sign(A1 E1)a2,y0 =___0, p0 = mink(pk)1, (p0 N A0) = mink(pk)___x0 = NDE1+|N|2y0NA1E1x1NA2E1x2NA0E1A0 E2 x1 = Sign(A2 E2)a1, x2 = +Sign(A1 E2)a2,y0 +y1 =___0, p0 = mink(pk)1, (p0 N A0) = mink(pk)___x0 = NDE2+|N|2(y0+y1)NA1E2x1NA2E2x2NA0E2Table 4b. Coecients for unique points of OBB-Triangle intersection for A0 Ej.L coecientsA1 E0 x0 = +Sign(A2 E0)a0, x2 = Sign(A0 E0)a2,y1 =___0, p0 = mink(pk)1, (p0 +N A1) = mink(pk)___x1 = NDE0|N|2y1NA0E0x0NA2E0x2NA1E0A1 E1 x0 = +Sign(A2 E1)a0, x2 = Sign(A0 E1)a2,y0 =___0, (p0 = mink(pk))1, (p0 N A1) = mink(pk)___x1 = NDE1+|N|2y0NA0E1x0NA2E1x2NA1E1A1 E2 x0 = +Sign(A2 E2)a0, x2 = Sign(A0 E2)a2,y0 +y1 =___0, p0 = mink(pk)1, (p0 N A1) = mink(pk)___x1 = NDE2+|N|2(y0+y1)NA0E2x0NA2E2x2NA1E2Table 4c. Coecients for unique points of OBB-Triangle intersection for A1 Ej.23L coecientsA2 E0 x0 = Sign(A1 E0)a0, x1 = +Sign(A0 E0)a1,y1 =___0, p0 = mink(pk)1, (p0 +N A2) = mink(pk)___x2 = NDE0|N|2y1NA0E0x0NA1E0x1NA2E0A2 E1 x0 = Sign(A1 E1)a0, x1 = +Sign(A0 E1)a1,y0 =___0, p0 = mink(pk)1, (p0 N A2) = mink(pk)___x2 = NDE1+|N|2y0NA0E1x0NA1E1x1NA2E1A2 E2 x0 = Sign(A1 E2)a0, x1 = +Sign(A0 E2)a1,y0 +y1 =___0, p0 = mink(pk)1, (p0 N A2) = mink(pk)___x2 = NDE2+|N|2(y0+y1)NA0E2x0NA1E2x1NA2E2Table 4d. Coecients for unique points of OBB-Triangle intersection for A2 Ej.4 Triangles4.1 Separation of TrianglesLet the rst triangle have vertices A0, A1, A2, edges E0 = A1 A0, E1 = A2 A0, E2 = E1 E0, andnormal N = E0 E1 (not necessarily unit length). Let the second triangle have vertices B0, B1, B2, edgesF0 = B1B0, F1 = B2B0, F2 = F1F0, and normal M = F0F1 (not necessarily unit length). DeneD = B0 A0.Triangles in 3D present an interesting problem for nonintersection by the separating axis approach. The setof potential separating axes depends on whether or not the triangles are parallel. If the two triangles areparallel but not coplanar, then the triangle normals will provide separating axes. However, if the trianglesare coplanar, then neither normal provides a separating axis. Moreover, cross products of pair of edges fromthe triangles are all normal vectors, so they do not yield separating axes. It turns out that for the coplanarcase, cross products of a triangle normal with the edges of the other triangle provide the potential separatingaxes.For nonparallel triangles, the potential separating axes are of the form A0 +sL where L is one of N, M, orEi Fj for i = 0, 1, 2 and j = 0, 1, 2. For parallel or coplanar triangles, L is one of N, N Ei, or N Fifor i = 0, 1, 2.24The projection distances of the triangle vertices without the division by the squared length of L arep0 = L (A0 A0) = 0p1 = L (A1 A0) = L E0p2 = L (A2 A0) = L E1(17)andq0 = L (B0 A0) = L Dq1 = L (B1 A0) = L (D+F0) = q0 +L F0q2 = L (B2 A0) = L (D+F1) = q0 +L F1.(18)Equations (17) and (18) lead to the entries in the table below. This table represents the potential separatingaxes for nonparallel triangles.L p0 p1 p2 q0 q1 q2N 0 0 0 N D q0 +N F0 q0 +N F1M 0 M E0 M E1 M D q0 q0E0 F0 0 0 N F0 E0 F0 D q0 q0 +M E0E0 F1 0 0 N F1 E0 F1 D q0 M E0 q0E0 F2 0 0 N F2 E0 F2 D q0 M E0 q0 M E0E1 F0 0 N F0 0 E1 F0 D q0 q0 +M E1E1 F1 0 N F1 0 E1 F1 D q0 M E1 q0E1 F2 0 N F2 0 E1 F2 D q0 M E1 q0 M E1E2 F0 0 N F0 N F0 E2 F0 D q0 q0 +M E2E2 F1 0 N F1 N F1 E2 F1 D q0 M E2 q0E2 F2 0 N F2 N F2 E2 F2 D q0 M E2 q0 M E2Table 5. Values for pi and qj for the nonintersection test mini(pi) >maxj(qj) or maxi(pi) < minj(qj) for noncoplanar triangles.The table for the potential separating axes for coplanar triangles is given below. The quantites in that table25are = |N|; Ei Fj = ijN for i = 0, 1 and j = 0, 1; and F0 F1 = N.L p0 p1 p2 q0 q1 q2N 0 0 0 N D q0 q0NE0 0 0 2NE0 D q0 +00

2q0 +01

2NE1 0 20 NE1 D q0 +10

2q0 +11

2NE2 0 22NE2 D q0 + (10 00)2q0 + (11 01)2NF0 0 00

210

2NF0 D q0 q0 +2NF1 0 01

211

2NF1 D q0 2q0NF2 0 (00 01)2(10 11)2NF2 D q0 2q0 2Table 6. Values for pi and qj for the nonintersection test mini(pi) >maxj(qj) or maxi(pi) < minj(qj) for coplanar triangles.4.2 Testing for Intersection of Noncoplanar TrianglesI consider only the noncoplanar case. For 3D collision systems, the objects may as well be formed fromtriangles (rather than polygons of four or more sides). Moreover, the objects tend to have volume, so if twoobjects are colliding and the contact occurs between two coplanar triangles, one from each object, an instantlater other triangles, (1) not in the same plane as the colliding triangles and (2) attached to the collidingtriangles, will transversely intersect. The collision system will detect those intersections as occurring betweennoncoplanar triangles.4.2.1 StationaryTesting for intersection amounts to processing the various potential separating axes. Any quantities thatare needed multiple times are calculated only once and only when needed. The basic separating axis testinvolves computing the triangle intervals [mini(pi), maxi(pi)] and [minj(qj), maxj(qj)] and then testing fornonintersection by comparing the two intervals.Axes N or M. One triangle projects to a single point p and the other projects to up to three points q,q + d0, and q + d1. The nonintersection test is: min(q, q + d0, q + d1) > p or max(q, q + d0, q + d1) < p.Pseudocode isif ( q > p ){if ( q+d0 > p && q+d1 > p )return no_intersection;}else if ( q < p ){26if ( q+d0 < p && q+d1 < p )return no_intersection;}Axes EiFj. The triangles each project to exactly two points, the rst with values 0 and p, the second withvalues q and q +d. The nonintersection test is: min(q, q +d0, q +d1) > max{0, p} or max(q, q +d0, q +d1) = 0 ){if ( (q < 0 && q+d < 0) || (q > p && q+d > p) )return no_intersection;}else{if ( (q > 0 && q+d > 0) || (q < p && q+d < p) )return no_intersection;}4.2.2 Constant VelocitiesThe code for stationary triangles needs to be modied slightly to handle the case of constant velocities. Thevelocity of the rst triangle is subtracted from the velocity of the second triangle so that all calculationsare done relative to a stationary rst triangle. If the triangle velocities are V0 and V1, dene the relativevelocity to be W = V1 V0. Let the initial time and nal time of the calculations be t = 0 and t = T.Axes N or M. One triangle projects to a single p-value. The other triangle projects to three movingq-values. Abstractly the problem is to show that min(q +tw, q +d0 +tw, q +d1 +tw) > p or max(q +tw, q +d0 +tw, q +d1 +tw) < p for t [0, T]. Pseudocode isif ( q > p ){if ( d0 >= 0 ){if ( d1 >= 0 ){min = q;if ( min+T*w > p )return no_intersection;}else{min = q+d1;if ( min > p && min+T*w > p )return no_intersection;}}27else if ( d1 p && min+T*w > p )return no_intersection;}else{min = q+d0;if ( min > p && min+T*w > p )return no_intersection;}}else if ( q < p ){if ( d0 max(0, p) or max(q+tw, q+d+tw) = 0 ){if ( q < 0 ){if ( d p ){if ( d >= 0 ){min = q;if ( min+T*w > p )return no_intersection;}else{min = q+d;if ( min > p && min+T*w > p )return no_intersection;}}}else{if ( q > 0 ){if ( d >= 0 ){min = q;if ( min+T*w > 0 )return no_intersection;}else{min = q+d;if ( min > 0 && min+T*w > 0 )return no_intersection;29}}else if ( q < p ){if ( d 0 for which there is at least one separating axis for any t [0, T),but for which no separating axis exists at time T. The idea is to test each potential separating axis and keeptrack of the time at which the intervals of projection intersect for the rst time. The largest such time isthe rst time at which the triangles intersect. Also, it is important to keep track of which side each of theintervals is relative to the other interval. Finally, knowing the separating axis associated with the maximumtime T allows us to reconstruct a point of intersection.4.3.2 Finding a Point of IntersectionIf T is the rst time of intersection, the problem is to nd a point P in the intersection of the two triangles.Since the triangles are not coplanar, the only possibilities for the set of intersections is a single point or aline segment. We need to solvex0E0 +x1E1 = D+y0F0 +y1F1 (19)where D = (B0 + TV1) (A0 + TV0); for xi with 0 x0 1, 0 x1 1, x0 + x1 1; and for yj with0 y0 1, 0 y1 1, y0 +y1 1.Equation (19) can be solved for each variable individually by crossing then dotting equation with the propervectors. The solutions arexi = 12i|N|2 (N DE1i + (N F0 E1i)y0 + (N F1 E1i)y1)yj = 12j|M|2 (M DF1 + (M E0 F1j)x0 + (M E1 F1j)x1)30for i = 0, 1 and j = 0, 1. Each of these equations denes the left-hand side as a linear function of thevariables in the right-hand side. The extreme values occur at the vertices of the triangular domain ofthe function: (0, 0), (1, 0), and (0, 1). The extreme values of the equations dene intervals bounding eachvariable, xi [min(xi), max(xi)] and yj [min(yj), max(yj)]. The interval end points aremin(xi) = 1|N|2 min ((1 2i)0(i), (1 2i)1(i), (1 2i)2(i)) ,max(xi) = 1|N|2 max ((1 2i)0(i), (1 2i)1(i), (1 2i)2(i)) ,min(yj) = 1|M|2 min ((1 2j)0(j), (1 2j)1(j), (1 2j)2(j)) ,max(yj) = 1|M|2 max ((1 2j)0(j), (1 2j)1(j), (1 2j)2(j)) ,(20)where 0(i) = N DE1i, 1 = 0(i)+N F0E1i, 2(i) = 0(i)+N F1E1i, 0(j) = M DF1j,1(j) = 0(j) +M E0 F1j, and 2(j) = 0(j) +M E1 F1j.In the following constructions of the rst point of intersection, if any of the variables is not uniquely con-strained by the derived equations, then such a variable can be selected from the intervals [min(xi), max(xi)]or [min(yj), max(yj)] and subject to triangular domain constraints for that variable.Last Separating Axis N. Dot equation (19) with N to obtain0 = N D+y0N F0 +y1N F1.The projection of the rst triangles vertices leads to a single p value of 0. The projection of the secondtriangles vertices leads to (possibly) distinct q values, {q0, q1, q2}, dened in Table 5. There are three casesto consider.The rst case is q0 = mini(qi) in which case = 1 or q0 = maxi(qi) in which case = 1. Then N D = 0,N F0 0, N F1 0, and0 = (N F0)y0 + (N F1)y1. (21)If N F0 = 0 and N F1 = 0, then y0 = 0 and y1 = 0 are required. If N F0 = 0 and N F1 = 0,equation (21) requires y1 = 0, but does not constraint y0. A point of intersection is provided by anyy0 [min(y0), max(y0)] [0, 1] where min(y0) and max(y0) are dened in equation (20). If N F0 = 0 andN F1 = 0, equation (21) requires y0 = 0, but does not constrain y1. A point of intersection is providedby any y1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are dened in equation (20). If bothN F0 = 0 and N F1 = 0, then N and M must be parallel and the triangles must be coplanar. While theassumption of this section is that the two vectors are not parallel, numerical error might generate this case.Discussion of intersection of coplanar triangles occurs later in this document.The second case is q1 = mini(qi) in which case = 1 or q1 = maxi(qi) in which case = 1. ThenN D = N F0, N F0 0, (N F1 N F0) 0, and0 = (N F0)(1 y0 y1) + [(N F1 N F0)]y1. (22)If N F0 = 0 and N F1 = N F0, then y0 + y1 = 1 and y1 = 0 are required, Therefore, y0 = 1 andy1 = 0. If N F0 = 0 and N F1 = N F0, equation (22) requires y1 = 0, but does not constrain y0.In this case y0 [min(y0), max(y0)] [0, 1] where min(y0) and max(y0) are dened in equation (20). IfN F0 = 0 and N F1 = N F0, equation (22) requires y0 +y1 = 1, but does not constrain y1. In this casey1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are dened in equation (20). Given a choice fory1, set y0 = 1y1. If both N F0 = 0 and N F1 = N F0, then N and M must be parallel and the triangles31must be coplanar. As before, the discussion of intersection of coplanar triangles is postponed until later inthis document.The third case is q2 = mini(qi) in which case = 1 or q2 = maxi(qi) in which case = 1. ThenN D = N F1, (N F0 N F1) 0, N F1 0, and0 = [(N F0 N F1)]y0 + (N F1)(1 y0 y1). (23)If N F0 = N F1 and N F1 = 0, then y0 = 0 and y0 +y1 = 1 are required. Therefore, y0 = 0 and y1 = 1.If N F0 = N F1 and N F1 = 0, equation (23) requires y0 +y1 = 1, but does not constrain y0. In this casey0 [min(y0), max(y0)] [0, 1] where min(y0) and max(y0) are dened in equation (20). Given a choice fory0, set y1 = 1 y0. If N F0 = N F1 and N F1 = 0, equation (23) requires y0 = 0, but does not constrainy1. In this case y1 [min(y1), max(y1)] [0, 1] where min(y1) and max(y1) are dened in equation (20). Ifboth N F0 = N F1 and N F1 = 0, then N and M must be parallel and the triangles must be coplanar.As before, the discussion of intersection of coplanar triangles is postponed until later in this document.Last Separating Axis M. Dot equation (19) with M to obtainx0M E0 +x1M E1 = M D.The projection of the rst triangles vertices leads to (possibly) distinct p values {p0, p1, p2}. The projectionof the second triangles vertices leads to a single q value, q0, all dened in Table 5. There are three cases toconsider.The rst case is p0 = mini(pi) in which case = 1 or p0 = maxi(pi) in which case = 1. Then M D = 0,M E0 0, M E1 0, and(M E0)x0 + (M E1)x1 = 0. (24)If M E0 = 0 and M E1 = 0, then x0 = 0 and x1 = 0 are required. If M E0 = 0 and M E1 = 0,equation (24) requires x1 = 0, but does not constrain x0. A point of intersection is provided by anyx0 [min(x0), max(x0)] [0, 1] where min(x0) and max(x0) are dened in equation (20). If M E0 = 0 andM E1 = 0, equation (24) requires x0 = 0, but does not constrain x1. A point of intersection is providedby any x1 [min(x1), max(x1)] [0, 1] where min(x1) and max(x1) are dened in equation (20). If bothM E0 = 0 and M E1 = 0, then N and M must be parallel and the triangles must be coplanar. While theassumption of this section is that the two vectors are not parallel, numerical error might generate this case.Discussion of intersection of coplanar triangles occurs later in this document.The second case is p1 = mini(pi) in which case = 1 or p1 = maxi(pi) in which case = 1. ThenM D = M E0, M E0 0, (M E1 M E0) 0, and0 = (M E0)(1 x0 x1) + [(M E1 M E0)]x1. (25)If M E0 = 0 and M E1 = M E0, then x0 + x1 = 1 and x1 = 0 are required. Therefore, x0 = 1 andx1 = 0. If M E0 = 0 and M E1 = M E0, equation (25) requires x1 = 0, but does not constrain x0.In this case x0 [min(x0), max(x0)] [0, 1] where min(x0) and max(x0) are dened in equation (20). IfM E0 = 0 and M E1 = M E0, equation (25) requires x0 +x1 = 1, but does not constrain x1. In this casex1 [min(x1), max(x1)] [0, 1] where min(x1) and max(x1) are dened in equation (20). Given a choice forx1, set x0 = 1 x1. If both M E0 = 0 and M E1 = M E0, then N and M must be parallel and thetriangles must be coplanar. As before, the discussion of intersection of coplanar triangles is postponed untillater in this document.The third case is p2 = mini(pi) in which case = 1 or p2 = maxi(pi) in which case = 1. ThenM D = M E1, (M E0 M E1) 0, M E1 0, and0 = [(M E1 M E0)]x0 + (M E1)(1 x0 x1). (26)32If M E0 = M E1 and M E1 = 0, then x0 = 0 and x0 + x1 = 1 are required. Therefore, x0 = 0 andx1 = 1. If M E0 = M E1 and M E1 = 0, equation (25) requires x0 +x1 = 1, but does not constrain x0.In this case x0 [min(x0), max(x0)] [0, 1] where min(x0) and max(x0) are dened in equation (20). Givena choice for x0, set x1 = 1 x0. If M E0 = M E1 and M E1 = 0, equation (25) requires x0 = 0, butdoes not constrain x1. In this case x1 [min(x1), max(x1)] [0, 1] where min(x1) and max(x1) are denedin equation (20). If both M E0 = M E1 and M E1 = 0, then N and M must be parallel and the trianglesmust be coplanar. As before, the discussion of intersection of coplanar triangles is postponed until later inthis document.Last Separating Axis Ei Fj. Let (i0, i1, i2) and (j0, j1, j2) be permutations of (0, 1, 2) in the set{(0, 1, 2), (1, 0, 2), (2, 1, 0)}. The functions , , , and are the same used in the section on nding inter-sections between OBBs and triangles.Dot equation (19) with Ei0 Fj0 to obtain(E0 Ei0 Fj0)x0 + (E1 Ei0 Fj0)x1 = D Ei0 Fj0 + (F0 Ei0 Fj0)y0 + (F1 Ei0 Fj0)y1.Using the various identities mentioned earlier, the equation reduces to(i0)(N Fj0)x0 +(i0)(N Fj0)x1 = D Ei0 Fj0 (j0)(M Ei0)y0 (j0)(M Ei0)y1. (27)The projection of the rst triangles vertices leads to two distinct p values, p0 = 0 and p1 = (i0)N Fj0.The projection of the second triangles vertices leads to two distinct q values, q0 = D Ei0 Fj0 andq1 = q0 (j0)M Ei0}. There are four cases to consider depending on which of the projection values areminima or maxima. In each case I derive the solutions when the intersection point is unique. Nonuniquenessis discussed after the four cases.The four cases each require two additional constraints on the variables. Dotting equation (19) with M andN yields equationsM E0x0 +M E1x1 = M DN F0y0 +N F1y1 = N D.(28)The rst case is min(q) = q0 and max(p) = 0 in which case = 1 or max(q) = q0 and min(p) = 0 in whichcase = 1. Then q0 = 0, (i0)N Fj0 0, and (j0)M Ei0 0. Equation (27) is equivalent to0 = [(i0)N Fj0][(i0)x0 +(i0)x1] + [(j0)M Ei0][(j0)y0 +(j0)y1].If N Fj0 = 0 and M Ei0 = 0, then (i0)x0 + (i0)x1 = 0 and (j0)y0 + (j0)y1 = 0 are required. Thesetwo constraints and equations (28) uniquely determine x0, x1, y0, and y1.The second case is min(q) = q0 and max(p) = (i0)N Fj0 in which case = 1 or max(q) = q0 andmin(p) = (i0)NFj0 in which case = 1. Then q0 = (i0)NFj0, (i0)NFj0 0, and (j0)MEi0 0.Equation (27) is equivalent to0 = [(i0)N Fj0][1 (i0)x0 (i0)x1] + [(j0)M Ei0][(j0)y0 +(j0)y1].If N Fj0 = 0 and M Ei0 = 0, then (i0)x0 + (i0)x1 = 1 and (j0)y0 + (j0)y1 = 0 are required. Thesetwo constraints and equations (28) uniquely determine x0, x1, y0, and y1.The third case is min(q) = q0(j0)MEi0 and max(p) = 0 in which case = 1 or max(q) = q0(j0)MEi0and min(p) = 0 in which case = 1. Then q0 = (j0)M Ei0, (i0)N Fj0 0, and (j0)M Ei0 0.33Equation (27) is equivalent to0 = [(i0)N Fj0][(i0)x0 +(i0)x1] + [(j0)M Ei0][1 (j0)y0 (j0)y1].If N Fj0 = 0 and M Ei0 = 0, then (i0)x0 + (i0)x1 = 0 and (j0)y0 + (j0)y1 = 1 are required. Thesetwo constraints and equations (28) uniquely determine x0, x1, y0, and y1.The fourth case is min(q) = q0 (j0)M Ei0 and max(p) = (i0)N Fj0 in which case = 1 or max(q) =q0 (j0)M Ei0 and min(p) = (i0)N Fj0 in which case = 1. Then q0 = (i0)N Fj0 + (j0)M Ei0,(i0)N Fj0 0, and (j0)M Ei0 0. Equation (27) is equivalent to0 = [(i0)N Fj0][1 (i0)x0 (i0)x1] + [(j0)M Ei0][1 (j0)y0 (j0)y1].If N Fj0 = 0 and M Ei0 = 0, then (i0)x0 + (i0)x1 = 1 and (j0)y0 + (j0)y1 = 1 are required. Thesetwo constraints and equations (28) uniquely determine x0, x1, y0, and y1.The coecients needed to produce the unique points of intersection are summarized in the following tables.L coecientsN y0 = 0, y1 = 0, q0 = mink(qk)y0 = 1, y1 = 0, q1 = mink(qk)y0 = 0, y1 = 1, q2 = mink(qk)M x0 = 0, x1 = 0, p0 = mink(pk)x0 = 1, x1 = 0, p1 = mink(pk)x0 = 0, x1 = 1, p2 = mink(pk)E0 Fj x1 = 0, x0 = M D/M E0, 0 = maxk(pk)x1 = 1, x0 = 0, 0 = mink(pk)E1 Fj x0 = 0, x1 = M D/M E1, 0 = maxk(pk)x0 = 1, x1 = 0, 0 = mink(pk)E2 Fj x0 = 0, x1 = 0, 0 = maxk(pk)x0 = (M E1 M D)/M E2, 0 = mink(pk)Table 7. Coecients for unique points of TRI-TRI intersection.5 Processing of Moving ObjectsConsider a rigid, moving object with a central point K(t) and a frame eld F(t), a 3 3 rotation matrixwhose columns represent a coordinate system with origin K(t), where t [0, T]. The frame eld does notnecessarily have to be the Frenet frame for the curve traversed by the central point. The path followed byany other point X0 is X(t) = K(t) + F(t)F(0)T(X0 K(0)). If G0 is a frame eld for X0, then the frameeld for X(t) is G(t) = F(t)F(0)TG0.34In particular, the path and orientation of each triangle in the mesh representing the object is derivable fromthe path and orientation of the central point of the object. The path of an OBB center and the axes of theOBB are similarly derivable. The tests for nonintersection of OBB-OBB, triangle-OBB, and triangle-trianglefor two rigid, moving objects are equivalent to showing that the minimum of a function of time with domain[0, T] is positive. For example, the nonintersection test of OBB-OBB is of the form R(t) R0(t) R1(t) > 0for t [0, T].Computing the minimum of a function can be an expensive operation, especially in the context of a dynamiccollision detection system that is used in a real-time environment. Rather than minimize the given functionf(t) for t [0, T], approximate f by a piecewise linear function by computing N 2 samples ti = iT/(N1)and fi = f(ti) for 0 i N 1. On each subinterval [ti, ti+1] the central point of the object is assumedto have constant velocity V = K(ti+1) K(ti). The nonintersection tests on the subinterval and withthe specied constant velocity are exactly those derived in the previous sections of this document. Theapplication can select N as desired, the choice probably dependent on frame rate (or number of cycles tospare per frame for the collision system).Of particular interest is the situation where the object motion is dened by the system of dierential equationsdXdt = V+W(XK0)where K0 is the initial location for the central point of the object, V is a constant tangential velocity, andW is a constant angular velocity. The length of V is the constant tangential speed and the length of W isthe constant angular speed. The solution to the dierential equation for initial condition X(0) = X0 isX(t) = K0 +tV+R(t, W)(X0 K0)where R(t, W) is a rotation matrix about the axis K0 + tW. The central path is K(t) = K0 + tV. Thedierence of the two paths X(t) and K(t) isX(t) K(t) = R(t, W)(X0 K0).The initial frame eld F(0) has colums of the form X0 K0, so the frame eld at other times is F(t) =R(t, W)F(0).Rather than compute the closed form for F(t) which requires evaluation of trigonometric functions, it isbetter to numerically integrate the system of dierential equations to obtain the sample positions. That is,solve dF/dt = SF where S is the skew symmetric matrix dened by the linear operation W X = SX.Eulers method is certainly the easies to apply, but nothing prevents you from using a higher-order methodsuch as Runge-Kutta. Regardless of numerical integrator, it is necessary to take the iterate representingF(t) and use Gram-Schmidt orthonormalization to guarantee that you have a rotation matrix to work with.6 Constructing an Oriented Bounding Box TreeGiven a triangular mesh consisting of a collection of vertices and a connectivity list, the basic approach toconstructing an OBB tree is recursive. An OBB is computed to contain the initial triangular mesh. Themesh is split ino two submeshes, the algorithm possibly using information about the OBB to determine howto split the mesh. If a submesh contains at least two triangles, then the process is repeated on that submesh.If a submesh has exactly one triangle, no OBB is constructed, but the triangle is considered to be at a leafnode of the tree.35The OBB nodes themselves must store various information to aid in collision detection. I assume that thetrimesh represents a rigid body. Dynamically morphed objects are problematic in that OBB trees wouldneed to be recomputed during runtime, an expensive operation. While there are many ways to organize thedata, the simplest is to require each OBB node to store a pointer to the rigid body object, an OBB, pointersto the two child OBB nodes, and an index to a triangle.The pointer to the object is used to query the object about motion information. For example, if the objectvelocity is a function of time, an OBB node may need to query the object to determine at a specic timewhat the velocity is. The pointers to the children are both not null for interior OBB nodes and both nullfor leaf OBB nodes. The index to a triangle is only used at OBB leaf nodes. This index is used in queryingthe object to get the actual triangle vertex data that is needed to compute triangle-triangle intersections.The tree generation algorithm also allows for building less than a full tree. An application can specify athreshold on the number of triangles for an OBB leaf node. The full tree has a single triangle per leaf node.However, if an application species at least two triangles per leaf node, the splitting algorithm will be appliedduring construction of an OBB node only if that node has more than two triangles in its mesh. The numberis heuristic in that an OBB node with three triangles is allowed to be split. The child with two triangles isno longer subdivided. The other child has a single triangle.6.1 Generating an OBB for a TrimeshA variety of methods can be used for computing an OBB for a triangular mesh. In real-time applications,these methods are applied in a preprocessing phase, so their execution times are not typically an issue. Thethree algorithms discussed are: minimum volume OBB, OBB based on distribution of mesh points, and OBBbased on distribution of mesh triangles. The last type of OBB is what is described in [1].6.1.1 Minimum Volume OBBIdeally this provides the best t of a mesh in the sense that the OBB requires the minimum volume of spaceof all the possible OBBs that can t the mesh. Whether or not this is the best practical choice I leave up tothe implementor.Given a collection of points Xj for 0 j < n, an OBB that ts the points can be constructed for eachchoice of coordinate axes Ai, i = 0, 1, 2. The points are projected onto the axes X0 +sAi, the values beingij = Ai (Xj X0) for all j. Dene i = minj(ij), i = maxj(ij), and i = (i + i)/2. The center ofthe smallest volume OBB with specied axes isC = X0 +2

i=0iAi.The extents of the OBB are ai = (i i)/2.Each set of coordinate axes can be represented as the columns of rotation matrices. Each rotation matrixis generated by a unit-length vector U and an angle [, 2]. The mapping from rotation matrices tocoordinate axes is of course not one-to-one. However, the volume of the OBBs can be viewed as a functionV : S2 [0, 2] [0, ) where S2is the unit sphere. The volume is V (U, ) = 2i=0(i i). Thisfunction is continuous on its compact domain, so from calculus it must attain its minimum on that domain.36Therefore, there exists an axis U0 and an angle 0 for which V (U0, 0) V (U, ) for all axes U and allangles .The construction of U0 and 0 can be implemented as a numerical minimization using techniques that donot require derivatives. A good choice is Powells direction set method, [3]. The rate of convergence to theminimum depends on initial guess for axis and angle. The algorithm discussed next provides a reasonableinitial guess for the minimizer.6.1.2 OBB from PointsAn OBB can be constructed by tting the mesh points Xj for 0 j < n with an anisotropic Gaussiandistribution. The center of the OBB is the mean of the points,C = 1nn

j=0Xj.The axes of the OBB are selected as unit-length eigenvectors of the covariance matrixM = 1nn1

j=0(Xj C)(Xj C)Twhere T indicates transpose. If Ai are unit-length eigenvectors, the extents along those axes are ai =maxj|Ai (Xj C)|.A minor variation that leads to a slightly better t is to compute just the eigenvectors of the covariancematrix and compute the intervals of projection as in the case of building a minimum volume OBB. Thecenter of the OBB is computed the same way as in the minimum volume algorithm.The main problem with this approach is that the box orientation can be heavily inuenced by mesh pointsthat are interior to the convex hull of the points. As in the algorithm for minimum volume OBB construction,the box orientation should depend only on the convex hull of the mesh points. The solution is to rst applya convex hull algorithm, then process only those points in the covariance matrix.6.1.3 OBB from TrianglesThe t of an OBB to the convex hull of the mesh points given previously still has problems with sampling.The mesh points on the convex hull may be irregularly distributed so that a small, dense collection of pointscan unfairly aect the orientation of the bounding box. This eect can be minimized by using a continuousformulation of the covariance matrix.Suppose there are triangles. If the ithtriangle has vertices V0,i, V1,i, and Vi,2, then the triangle andits interior are represented by Xi(s, t) = V0,i + s(V1,i V0,i) + t(V2,i V0,i) for 0 s 1, 0 t 1,and s + t 1. Let mi = |(V1,i V0,i) (V2,i V0,i)|/2 be the area of the triangle. Dene the weightswi = mi/

1i=0 mi. The mean point of the convex hull isC = 2

1i=0 wi_10_1t0 Xi(s, t) ds dt= 13

1i=0 wi_

2j=0Vj,i_37and the covariance matrix of the convex hull isM = 2

1i=0 wi_10_1t0 (Xi(s, t) C)(Xi(s, t) C)Tds dt= 112

1i=0 wi_

2j=0

2k=0(Vj,i C)(Vk,i C)T_.If Ai are unit-length eigenvectors, the extents along those axes are ai = maxj|Ai (Xj C)| where theXj are the vertices. As in the section on tting points with a Gaussian distribution, a variation allowsadjustment of C once the axes Ai are known.6.2 Splitting a TrimeshGiven a triangular mesh with corresponding oriented bounding box, the mesh can be split into two submeshes.The idea is to split the OBB by a plane orthgonal to the longest axis of the box, then partition the trianglesbased on which side of the splitting plane their centers lie. There are many heuristics for location of splittingplane, but I present only two.The rst algorithm uses the splitting plane orthogonal to the longest axis and passing through the center ofthe OBB. Because of variations in triangle size, this algorithm may not produce a balanced tree. Worse isthat it may not provide a subdivision in that all the triangle centers occur on the same side of the plane. Ifthe longest axis does not partition the triangles, the next longest axis can be used. If in turn this does notpartition the triangles, then the last axis is used. If all three axes fail to partition the triangles, then someother criterion for splitting must be used.The second algorithm uses the splitting plane orthogonal to the longest axis and passing through that pointcorresponding to the median value of the projection of the triangle centers onto the longest axis. Thisguarantees that the tree is balanced, a desirable trait since it keeps the height of the tree small compared tothe number of triangles represented by the tree.7 A Simple Dynamic Collision Detection SystemThere are many choices for testing for collisions between two OBB trees. I present one simple methodthat implements a dual recursion on the two OBB trees and compares OBBs and triangles for collisions.Eectively an OBB of one tree is compared against an OBB of the other tree. If the two OBBs intersect,then the children of the second OBB are compared against the current OBB of the rst tree.The algorithms assume a function bool HasObb (ObbTree node) that returns true if and only if the nodehas an associated OBB. It also assumes a functionbool HasChildren (ObbTree node, int depth){return ( Exists(node.Lchild) && Exists(node.Rchild) && depth != 0 );}Having children is necessary but not sucient for this function to return true. The test on depth supportslimiting the depth of traversal. The application specifes a positive depth to limit the traversal. To get afull traversal, the application species the depth to be a negative number. The depth is decremented for38each recursive call of TestCollisions, so in the case of an initial positive depth, any visited node for whichcurrent depth value is zero is considered a leaf node. For an initial negative depth, the test for children isunaected by the subsequent depth values. The semantics of HasChildren precludes the calls to HasObbbeing replaced by calls to HasChildren.7.1 Testing for CollisionThe method TestIntersection calls the appropriate intersection routine based on whether or not the treenode is interior or leaf. The returned value is true if and only if the corresponding OBBs or trianglesintersect during the specied time interval. Motion parameters are maintained by the object whose pointeris stored in the OBB nodes and can be accessed within the intersection calls.bool TestIntersection (float dt, ObbTree node0, ObbTree node1){if ( HasObb(node0) ){if ( HasObb(node1) )return ObbObbIntersect(dt,node0.Obb,node1.Obb);elsereturn ObbTriIntersect(dt,node0.Obb,node1.Tri);}else{if ( HasObb(node1) )return TriObbIntersect(dt,node0.Tri,node1.Obb);elsereturn TriTriIntersect(dt,node0.Tri,node1.Tri);}}The values depth0 and depth1, when passed to the TestCollision for the root nodes of the OBB trees,are the application-specied maximum depths of traversal for the OBB trees. The returned value forTestCollision is true if and only if the two subtrees that are rooted at the input nodes do intersect.bool TestCollision (float dt, ObbTree node0, int depth0, ObbTree node1, int depth1){if ( !TestIntersection(dt,node0,node1) )return false;if ( HasChildren(node0,depth0) ){if ( TestCollision(dt,node0.Lchild,depth0-1,node1,depth1) )return true;if ( TestCollision(dt,node0.Rchild,depth0-1,node1,depth1) )return true;if ( HasChildren(node1,depth1) ){39if ( TestCollision(dt,node0,depth0,node1.Lchild,depth1-1) )return true;if ( TestCollision(dt,node0,depth0,node1.Rchild,depth1-1) )return true;}return false;}if ( HasChildren(node1,depth1) ){if ( TestCollision(dt,node0,depth0,node1.Lchild,depth1-1) )return true;if ( TestCollision(dt,node0,depth0,node1.Rchild,depth1-1) )return true;return false;}return true;}The last line of the function returns true since both node0 and node1 are at the end of the recursive calls andthe call to TestIntersection already has shown that the corresponding OBBs or triangles are intersecting.Also note that the semantics of this routine say that if the traversal is limited by an application-specieddepth, an intersection between two OBBs or between an OBB and a triangle is counted as a collision, evenif the underlying trimesh geometry does not intersect. This illustrates once again the trade-o betweenaccuracy and compute time.7.2 Finding Collision PointsThe method FindIntersection calls the appropriate intersection routine based on whether or not the treenode is interior or leaf. A returned value is true if the collision system is to continue searching othercollisions. The value does not indicate that there is an intersection point between the OBBs, OBB andtriangle, or triangles.Any intersection points found by FindIntersection when applied to OBBs or triangles are passed onto theapplication via a callback mechanism that is associated with the object whose pointer is stored by the OBBnode. Normal vectors are also passed to the callback. A normal for an OBB is computed as if the OBB werean ellipsoid, thus providing a smoothed normal vector eld for the box. The return value of the callback isboolean and indicates whether or not the collision system should continue searching for collisions. This givesthe application the opportunity to terminate the search after one or more collisions rather than processingall possible collision points.bool FindIntersection (float dt, ObbTree node0, ObbTree node1){// first time, location, and normals of intersectionfloat time;Point3 intersect, normal0, normal1;40if ( HasObb(node0) ){if ( HasObb(node1) ){FindObbObb(dt,node0.Obb,node1.Obb,time,intersect);node1.Obb.GetNormal(intersect);}else{FindObbTri(dt,node0.Obb,node1.Tri,time,intersect);node1.Tri.GetNormal(intersect);}normal0 = node0.Obb.GetNormal(intersect);}else{if ( HasObb(node1) ){FindTriObb(dt,node0.Tri,node1.Obb,time,intersect);node1.Obb.GetNormal(intersect);}else{FindTriTri(dt,node0.Tri,node1.Tri,time,intersect);node1.Tri.GetNormal(intersect);}normal0 = node0.Tri.GetNormal(intersect);}}// provide the application with the collision informationbool bContinue0;if ( node0.Object.Callback ){bContinue0 = node0.Object.Callback(node1.Object,time,intersect,normal0,normal1);}else{bContinue0 = true;}bool bContinue1;if ( node1.Object.Callback ){41bContinue1 = node1.Object.Callback(node0.Object,time,intersect,normal1,normal0);}else{bContinue1 = true;}return bContinue0 && bContinue1;Pseudocode for nding a point of intersection is given below. The return value is true if and only if thecollision system should continue searching for collisions.bool FindCollision (float dt, ObbTree node0, int depth0, ObbTree node1, int depth1){if ( !TestIntersection(dt,node0,node1) )return true;if ( HasChildren(node0,depth0) ){if ( !FindCollision(dt,node0.Lchild,depth0-1,node1,depth1) )return false;if ( !FindCollision(dt,node0.Rchild,depth0-1,node1,depth1) )return false;if ( HasChildren(node1,depth1) ){if ( !FindCollision(dt,node0,depth0,node1.Lchild,depth1-1) )return false;if ( !FindCollision(dt,node0,depth0,node1.Rchild,depth1-1) )return false;}return true;}if ( HasChildren(node1,depth1) ){if ( !FindCollision(dt,node0,depth0,node1.Lchild,depth1-1) )return false;if ( !FindCollision(dt,node0,depth0,node1.Rchild,depth1-1) )return false;return true;}// At this point we know there is an intersection. Compute the// intersection and make this information available to the application// via the object callback mechanism.return FindIntersection(dt,pkTree1);42}References[1] Stefan Gottschalk, Ming Lin, and Dinesh Manocha, OBBTree: A Hierarchical Structure for RapidInterference Detection, In Proceedings of ACM Siggraph, pp. 171-180, 1996.[2] Tomas Moller, A Fast Triangle-Triangle Intersection Test, Journal of Graphics Tools, vol. 2, no. 2, pp.25-30, 1997.[3] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, Numerical Recipes in C: The Art ofScientic Computing, Cambridge University Press, Cambridge, England, 198843Eigensystems for 3 3 Symmetric Matrices (Revisited)David EberlyGeometric Tools, LLChttp://www.geometrictools.com/Copyright c 1998-2008. All Rights Reserved.Created: October 9, 2006Last Modied: March 2, 2008Contents1 About the Previous Version of This Document 22 The Motivation 23 Roots of Cubic Polynomials 33.1 The General Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33.2 The Characteristic Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43.3 Understanding the Numerical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.4 Computing the Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Computing the Eigenvectors 84.1 Theoretical Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.1.1 Three Distinct Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.1.2 Two Distinct Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.1.3 One Distinct Eigenvalue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.1.4 Theoretical Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.2 Numerical Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.3 An Alternate Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Performance Measurements 2111 About the Previous Version of This DocumentThe previous version of this document has been a quite popular download and a source of the question: Doyou have an implementation to download? I nally decided to implement the method, knowing that themathematics is simple yet realizing the classic problem with oating-point arithmetic would rear its uglyhead. Indeed, it did and led to my rewriting this document.The previous version had one inaccuracy, which had to be resolved for an implementation. The 33 symmet-ric matrix A has the characteristic equation p() = det(AI) = 0, where p() is a cubic polynomial. Theq variable that is calculated during the polynomial root construction is rst used to classify the multiplicityof the roots. I mentioned that the case q > 0 meant there is exactly one real-valued root to p() = 0, whichimplies A is diagonal. This is incorrect; the correct interpretation is that the cubic polynomial has onereal-valued root and two non-real-valued roots (complex-valued roots with nonzero imaginary parts). Forthe problem at hand, all the roots must be real-valued. As it turns out, the only choices for the eigensolverare q < 0 (three distinct real-valued roots) or q = 0 (all real-valued roots, one having at least a multiplicityof 2).The numerical problems rst occur in computing the roots, as expected, but can be dealt with appropriately.However, eigenvector construction also must be handled carefully. It is necessary to compute correctly therank of the matrix M = AI, a number that is 0, 1, or 2 (and not 3 since M is necessarily singular).2 The MotivationA standard result from linear algebra is that an n n symmetric matrix A with real-valued entries musthave n real-valued and unit-length eigenvectors V1 through Vn that are mutually orthogonal. Each vectorsatises the eigenvector equation, AVi = iVi, where i is the eigenvalue associated with the eigenvector.The eigenvalues are not necessarily distinct. Let R = [V1 Vn], where the columns of the matrixare the eigenvectors; this matrix is orthogonal. Also, let D = Diag(1, . . . , n), a diagonal matrix whosediagonal entries are the eigenvalues. The n eigenvector equations can be written as a single matrix equation,AR = RD. Since R is orthogonal, R1= RT, so equivalently A = RDRT.Various iterative numerical methods may be applied to A to extract the eigenvectors (stored in R) andeigenvalues (stored in D). One method uses Jacobi transformations to approximate R by a compositionof rotation matrices, Q = Q1 Qk, with k large enough so that QTAQ is eectively diagonal. Anothermethod uses either Givens reductions or Householder reductions to obtain a matrix, Q, in a xed number ofsteps so that B = QTAQ is tridiagonal. The matrix B is then factored to RDRTusing an iterative schemesuch as the QR or QL algorithms. These methods are designed to be accurate and robust.When n = 3, the polynomial p() = det(A I) has degree 3. Closed-form equations exist for the roots ofthe polynomial, so in theory it is possible to construct the eigenvalues and eigenvectors using a noniterativeapproach. An application that frequently computes the eigenvalues and eigenvectors of A will benet fromthis approach if the computational time is less than that of the iterative approach. At the same time, thenoniterative approach must be numerically robust.23 Roots of Cubic PolynomialsWe rst look at the construction of roots to any cubic polynomial with real-valued coecients. Those cubicpolynomials obtained as characteristic polynomials det(AI) have some unique properties that allow theroot nding to be specialized.3.1 The General EquationConsider the cubic polynomial equation 3 c22+ c1 c0 = 0, where the coecients c0, c1, and c2 arereal-valued numbers. The squared term may be eliminated by the change of variables, = +c2/3, leadingto the equation 3+ a + b = 0, wherea = 3c1 c223 , b = 2c32 + 9c1c2 27c027 , q = b24 + a327 (1)The quantity q is not part of the coecients of the -polynomial, but it is used in the equations for theroots. There are a few cases to consider for the root construction. The -roots are computed; the -rootsare obtained from the change of variables = + c2/3.Case 1. Let a = 0 and b = 0. The polynomial equation is 3= 0, so zero is a root of multiplicity 3. The-roots are0 = 1 = 2 = 0Case 2. Let a = 0 and b = 0. The polynomial equation is 3= b. Dene = (b)1/3, the real-valuedcube root of b. If b > 0, the -roots are0 = , 1 = (cos(/3) + i sin(/3)), 2 = (cos(/3) i sin(/3))If b < 0, the -roots are0 = , 1 = (cos(2/3) + i sin(2/3)), 2 = (cos(2/3) i sin(2/3))Case 3. Let a = 0 and b = 0. The polynomial equation is 3+ a = 0. The -roots are0 = 0, 1 = a, 2 = aCase 4. Let a = 0 and b = 0. The trick to solving the equation is to make use of a trigonometric identity,4 cos3 3 cos cos(3) = 0Make the change of variables = cos to obtain3cos3 + a cos + b = 0The left-hand side has a form similar to that of the trigonometric identity as long as (3, a, b) is a vectorparallel to (4, 3, cos(3)). To be parallel, the cross product must be the zero vector,(0, 0, 0) = (4, 3, cos(3)) (3, a, b) = (a cos(3) 3b, 3cos(3) 4b, (4a + 32))3When is not zero, the last and rst components may be solved for = 2

a3 , cos(3) = 3baIf a = 0, then = 0. If a < 0, then = 2

|a|/3, which is a real-valued number. However, if a > 0, then = 2

|a|/3 i, which is pure imaginary (non-real-valued).The equation cos(3) = 3b/(a) poses a more subtle problem. If is pure imaginary, we must resort tocomplex analysis to interpret what it means for the cosine of an angle to equal a complex number. Even if is real-valued, we still need to resort to complex analysis. This is evident when |2b/(a)| > 1. We know thatfor real numbers, | cos(3)| 1. When using complex numbers, it is possible for the complex-valued cosine tohave magnitude larger than 1. the complex-valued sine function is dened by sin(z) = (eizeiz)/(2i) and thecomplex-valued cosine function is dened by cos(z) = (eiz+ eiz)/2. Thus, the equation cos(3) = 3b/(a)may be solved using complex numbers using these denitions. That said, notice that if is a solution to theequation, then so are + 2/3 and + 4/3. In summary, the -roots are0 = cos(), 1 = cos( + 2/3), 2 = cos( + 4/3)In an implementation, three cosine evaluations are avoided by using a single sine and a single cosine evalua-tion. That is, cos(+2/3) = (1/2) cos()(3/2) sin() and cos(+4/3) = (1/2) cos()+(3/2) sin().All Cases. The four cases may be combined, using some algebraic manipulation, to the following. Deneu = (b/2 +q )1/3, v = (b/2 q )1/3(2)The principal square roots and cube roots are implied by the expressions. The -roots are0 = u + v, 1 = u + v2 + u v23, 2 = u + v2 u v23 (3)Even though the term 3 appears in the expressions, the roots can still be real-valued. This is the casewhen u v is a pure imaginary number. Also notice the absence of explicit sine and cosine functions. Infact, these functions will appear when computing the cube roots associated with u and v.A closer analysis of Equations (1), (2), and (3) will lead to the classicationq > 0, one real root, two conjugate complex rootsq = 0, three real roots of which at least two are equalq < 0, three distinct real roots(4)3.2 The Characteristic EquationTo match the notation of the previous section, we will consider the negated characteristic equation,0 = det(AI) = deta00 a01 a02a01 a11 a12a02 a12 a22 = 3c22+ c1 c04wherec0 = a00a11a22 + 2a01a02a12 a00a212 a11a202 a22a201c1 = a00a11 a201 + a00a22 a202 + a11a22 a212c2 = a00 + a11 + a22The roots of the cubic polynomial may be computed as shown in the previous section for the general equation.From that section, the relationship = a/3 leads tocos(3) = 3ba = b/2(a/3)

a/3Thinking of the right-hand side of the cos(3) equation as a ratio of the opposite side of a triangle to itshypotenuse, the adjacent side has a value

((a/3)

a/3)2(b/2)2= qWe may instead compute the tangent of 3 as the ratio of opposite to adjacent. To compute in the correctquadrant, though, an implementation will use instead atan2 and compute = atan2(q, b/2)/3An ANSI implementation of atan2 will return zero when its two arguments are both zero, so there is noneed to trap this special case. The roots are therefore0 = c2/3 + 2 cos(), 1 = c2/3

cos() +3 sin()

, 2 = c2/3

cos() 3 sin()

(5)To revisit the classication of roots, consider the following. If a = 0, then = 0 and the roots are0 = 1 = 2 = c2/3It is necessarily the case that b = 0 and q = 0. If a = 0, it is necessarily the case that a < 0. Suppose thatalso q = 0. If b 0, it must be that = 0, in which case the roots are0 = c2/3 + 2, 1 = c2/3 , 2 = c2/3 If instead b > 0, it must be that = /3, in which case the roots are0 = c2/3 + , 1 = c2/3 , 2 = c2/3 + In either case, there are two distinct roots, one of them repeated. If q = 0, it must be that q < 0. The threereal-valued roots of Equation (5) are distinct.3.3 Understanding the Numerical IssuesNow let us take a closer look at the a, b, and q values for polynomials with only real-valued roots. This willhelp us understand how to deal with the numerical problems that arise when attempting to compute theroots using oating-point arithmetic.5Let the real-valued roots be 0, 1, and 2, not necessarily distinct. For the sake of argument, let the rootsbe ordered by 0 1 2. The polyonomial isp() = ( 0)( 1)( 2)= 3(0 + 1 + 2)2+ (01 + 02 + 12) (012)= 3c22+ c1 c0Equating the ci with the root expressions and substituting into Equation (1) leads toa = ((1 0)2+ (2 0)2+ (2 1)2)/6b = (20 1 2)(0 21 + 2)(0 1 + 22)/27q = (1 0)2(2 0)2(2 1)2/108(6)These expressions make it clear that a 0 and q 0. For a to be zero, all the roots must be equal. For qto be zero, at least two roots must be equal. Any of three conditions makes b zero. The rst condition is0 = (1+2)/2, which states that 0 is the average of 1 and 2. Since 0 is the smallest root, the only wayit can be the average is if 0 = 1 = 2. The second condition is 2 = (0 + 1)/2, which similarly implies0 = 1 = 2. The third condition is 1 = (0+2)/2, which can happen even when there are three distinctroots (q < 0). Notice that b = 0 and q < 0 imply = /2, in which case the roots are 0 = c2/3 3,1 = c2/3, and 2 = c2/3 + 3.The maximum dierence of the roots is = 2 0. Dene 1 0 = and 2 1 = (1 ); it isnecessary that [0, 1]. An algebraic construction will show thata = (2 + 1)2/3 [2/3, 2/4]b = (23+ 32+ 3 2)3/27 [23/27, 23/27]q = 2(1 )26/108 [6/108, 6/1728](7)The calculations that are the least robust are for a root that is repeated two or three times, in theory, butthe numerical round-o errors in computing c0, c1, c2, a, b, and q make it appear as if the roots are alldistinct (and nearly the same oating-point values).In the case of three repeated roots, theoretically = 0 but numerically is a very small oating-pointnumber. Theoretically, a = 0 and q = 0 but numerically a = O(2) and q = O(6). You expect both a andq to be nearly zero with q a much smaller quantity than a.In the case of two distinct roots, say 0 = 1 < 2, theoretically = 0 but numerically is a small oating-point number. is relatively large in comparison, so you expect that a is suciently far away from zero (itlooks like 2/3). However, you expect that q is nearly zero (it is O(2)).There are a few sources of numerical round-o errors that cause the problems. First, the entries of Aalready can reect round-o errors in whatever process was using A. For example, if you choose a diagonalmatrix D = dI for some scalar d, you may think of its diagonal entries as exactly the eigenvalues you seek(multiplicity 3). Now numerically generate a rotation matrix R and compute A = RDRT. Theoretically, A =D but numerically the o-diagonal terms are numbers very close to zero. This can lead to the constructionof three distinct roots, all of them nearly the same oating-point number. Second, the computation of c0, c1,and c2 can have some subtractive cancellation that aects further computations. Third, the computation of6a, b, and q also are subject to subtractive cancellation. The worst oenders are the computations sqrt(-a/3)and sqrt(-q). If the arguments are theoretically zero but numerically nonzero and on the order of 106, anumber that is eectively zero when using single-precision oating-point arithmetic, the square roots are onthe order of 103. Thus, the error is magnied by these operations.If you precondition A by dividing by its maximum magnitude entry when that maximum is larger than 1, andif you use high-precision calculations, the total of all these round-o errors tend not to aect the accuracyof the root calculations. The problems, however, show up when you attempt to construct the eigenvectorsby solving the equations (A I)V = 0. It is important to correctly compute rank(A I). Small errorsin the eigenvalue calculations can lead to misclassication of the rank.3.4 Computing the RootsAs mentioned, A is preconditioned by dividing by its maximum magnitude entry when that maximum islarger than 1. The roots are computed in double precision. The pseudocode is shown below. The globalvariables are inv3, which is computed once as 1/3, and root3, which is computed once as 3.void ComputeRoots (Matrix3 A, double root[3]){double a00 = (double)A[0][0];