Game Development Reference
In-Depth Information
To determine if and where a ray
p
(
t
)=
p
0
+
t
u
intersects a sphere, we
plug the ray into an implicit sphere equation and solve for the parame-
ter
t
that satisfies the sphere equation, giving us the parameter that
yields the intersection point(s).
Plugging the ray into the sphere equation:
p
t
c
r
0
p
t
u
c
r
0
0
...from which we obtain the quadratic equation:
2
At
Bt
C
0
where
A
=
u
·
u
,
B
=2(
u
·(
p
0
-
c
)), and
C
=(
p
0
-
c
)·(
p
0
-
c
)-
r
2
.If
u
is normalized, then
A
=1.
Assuming
u
is normalized, we solve for
t
0
and
t
1
:
B
B
2
4
C
B
B
2
4
C
t
t
0
1
2
2
Figure 15.4 shows the possible results for
t
0
and
t
1
and shows what
these results mean geometrically.
Figure 15.4: a) The ray misses the sphere; both
t
0
and
t
1
will result in
imaginary solutions. b) The ray is in front of the sphere; both
t
0
and
t
1
will be negative. c) The ray is inside the sphere; one of the solutions
will be positive and one will be negative. The positive solution yields
the single intersection point. d) The ray intersects the sphere; both
t
0
and
t
1
are positive. e) The ray is tangent to the sphere, in which case
the solutions are positive and
t
0
=
t
1
.
The following method returns true if the ray passed in intersects the
sphere passed in. It returns false if the ray misses the sphere:
bool PickApp::raySphereIntersectionTest(Ray* ray,
BoundingSphere* sphere)
{
D3DXVECTOR3 v = ray->_origin - sphere->_center;
Search WWH ::
Custom Search