This section describes the ray-box intersection algorithm.
The following figure shows a graph with line equations:
Figure 6-22 Graph with line equations
Equation of a line
y = mx + b
The vector form of this equation is:
r = O + t*D
Where:
O
is the origin point
D
is the direction vector
t
is the parameter
The following figure shows an axis aligned bounding box:
Figure 6-23 Axis aligned bounding box
An axis aligned bounding box AABB
can
be defined by its min and max points A
and B
AABB
defines a set of lines parallel to
coordinate axis. Each component of line can be defined by the following equation:
x = Ax; y = Ay; z = Az
x = Bx; y = By; z = Bz
To find where a ray intersects one of those lines, equal both equations.
For example:
Ox + tx*Dx = Ax
You can write the solution as:
tAx = (Ax – Ox) / Dx
Obtain the solution for all components of both intersection points in
the same manner:
tAx = (Ax – Ox) / Dx
tAy = (Ay – Oy) / Dy
tAz = (Az – Oz) / Dz
tBx = (Bx – Ox) / Dx
tBy = (By – Oy) / Dy
tBz = (Bz – Oz) / Dz
In vector form these are:
tA = (A – O) / D
tB = (B – O) / D
This finds where the line intersects the planes defined by the faces of
the cube but it does not guarantee that the intersections lie on the cube.
The following figure shows a 2D representation of ray-box intersection:
Figure 6-24 Ray-box intersection 2D
representation
To find what solution is really an intersection with the box, you
require the greater value of the t
parameter for the
intersection at the min plane.
tmin = (tAx> tAy) ? tAx: tAy
You require the smaller value of the parameter t
for the intersection at the max plane.
tmin = (tAx> tAy) ? tAx: tAy
You also must consider those cases when you get no intersections.
The following figure shows a ray-box with no intersection:
Figure 6-25 Ray-box with no intersection
If you guarantee that the reflective surface is enclosed by the BBox,
that is, the origin of the reflected ray is inside the BBox, then there are always two
intersections with the box, and the handling of different cases is simplified.
The following figure shows a ray-box intersection in BBox:
Figure 6-26 Ray-box intersection in BBox