Game Development Reference
In-Depth Information
6.2.3 Intersection of a Ray and a Sphere
A sphere of radius r centered at the origin is described by the equation
2
2
2
2
.
(6.65)
x
++=
yz
r
()
Substituting the components of the ray
P
t
in Equation (6.51) for x , y , and z
gives us
(
)
(
)
2
2
(
)
2
2
StV
+
+
StV
+
+
StV
+
=
r
.
(6.66)
x
x
y
y
z
z
Expanding the squares and collecting on t yields the following quadratic
equation.
(
)
2
2
2
2
(
)
2
2
2
2
VVVt
++
+
2
SVSVSVt
+
+
+++−=
SSSr
0
(6.67)
x
y
z
x
x
y
y
z
z
x
y
z
The coefficients a , b , and c used in Equation (6.2) can be expressed in terms of
the vectors S and V as follows.
aV
b
cS r
=
=⋅
=−
2
(
)
2
SV
2
2
(6.68)
D
=−
b c
2
4
Calculating the discriminant
tells us whether the ray intersects the
sphere. As illustrated in Figure 6.2, if
<
0
, then no intersection occurs; if
=
,
D
D
0
then the ray is tangent to the sphere; and if
, then there are two distinct
points of intersection. If the ray intersects the sphere at two points, then the point
closer to the ray's origin S , which corresponds to the smaller value of t , is always
given by
>
D
0
−−
bD
t
=
(6.69)
2
a
because a is guaranteed to be positive.
The intersection of a ray and an ellipsoid can be determined by replacing
Equation (6.65) with the equation
2
2
2
2
2
2
x
+
my
+
nz
=
r
,
(6.70)
where m is the ratio of the x semiaxis length to the y semiaxis length, and n is the
ratio of the x semiaxis length to the z semiaxis length. Plugging the components