10.9 Linear Component to Triangle, Rectangle, Tetrahedron, Oriented Box 455
qPrime.y = box.extents.y;
}
if (line.origin.z < - box.extents.z) {
delta = line.origin.z + box.extents.z;
distanceSquared += delta * delta;
qPrime.z = -box.extents.z;
} else if (line.origin.z > box.extents.z) {
delta = line.origin.z - box.extents.z;
distanceSquared += delta * delta;
qPrime.z = box.extents.z;
}
return distanceSquared;
}
One Zero-Component
In this case, the transformed line is perpendicular to only one of the basis vectors.
Figure 10.49 shows a line that is perpendicular to the z-axis; the line may be above,
within, or below the OBB. Note that in this case, if the line does not intersect the
OBB, the closest point is always a corner.
The implementation of this can be highly optimized. Assume for the moment that
the (transformed) line’s direction vector
d
has only positive components; in this case,
if the line doesn’t intersect the box, then the closest point will be either the upper-left
or lower-right corner of the box, as can be seen in Figure 10.49. We can determine
which of these cases is possible by looking at the angle between the vector e going
from the upper-right corner to the line’s origin and the line’s direction vector
d
.The
Kross function discussed in Section 7.1 can be applied to these two vectors in order to
determine whether the angle between them is positive or negative; if Kross(e,
d
)>0,
then the line will intersect the “x-axis”; otherwise it will intersect the “y-axis.” In the
former case, the closest point on the box (if no intersection occurs) will be at the
lower-right corner, and in the latter case, the closest point will be at the upper-left
corner. Figure 10.50 demonstrates this. Once this has been determined, we need to
determine whether or not the line intersects the box. Consider the case where the
lower-right corner may be the closest point: If the angle between the line and a vector
from the closest corner to the line’s origin is positive, then the line will not intersect
the box; if the angle is negative, the line will intersect the box. Taking the dot product
of these vectors will not work, as the cosine function is symmetric about 0; however,
we can use the Kross function on the “perp” of the line and the corner-to-line-origin
vector, as shown in Figure 10.51. If the line intersects the box, then its distance is
0; otherwise, the distance squared is the distance between Q
and the transformed
line.