Game Development Reference
In-Depth Information
Figure 7.5
Two spheres intersect (a) and do not intersect (b).
The algorithm for this is only a couple of lines of code, as shown in
Listing 7.1
.
It's also extremely efficient, which is what makes using spheres a very popular ba-
sic collision detection option.
Listing 7.1
Sphere versus Sphere Intersection
function
SphereIntersection(
BoundingSphere
a
,
BoundingSphere
b
)
// Construct a vector between centers, and get
length squared
Vector3
centerVector
=
b
.
center
-
a
.
center
// Recall that the length squared of v is the
same as v dot v
float
distSquared
= DotProduct(
centerVector
,
centerVector
)
// Is distSquared < sum of radii squared?
if
distSquared
< ((
a
.
radius
+
b
.
radius
) * (
a
.
ra-
dius
+
b
.
radius
))
return true
else