A Simple Method For Box-Sphere Intersection Testing
A Simple Method For Box-Sphere Intersection Testing
V.8
A SIMPLE METHOD FOR
B OX – SPHERE
I NTERSECTION TESTING
James Arvo
Apollo Systems Division of Hewlett-Packard
Chelmsford, Massachusetts
Introduction
There are a number of computer graphics applications in which it is
necessary to determine whether a sphere intersects an axis-aligned paral-
lelepiped, or “box.” In two dimensions this arises when rasterizing
circles, which are two-dimensional spheres, into rectangular viewports,
which are two-dimensional boxes. The intersection test is used to deter-
mine whether any portion of the circle is indeed visible before rasterizing.
In three dimensions this operation may arise in spatial subdivision tech-
niques that identify “voxels,” or 3D boxes, pierced by the surfaces of
various objects. If spheres are among the objects, or are used as bounding
volumes for other objects, we need to test for 3D box-sphere intersec-
tion.
This note describes a simple method for detecting intersections of
n-dimensional boxes with n-dimensional spheres taken as either surfaces
or solids. Applications for n > 3 are not addressed here, though the
algorithm works in any dimension. For greatest generality all algorithms
shown below take the dimension of the space, n, as an input parameter,
though typical implementations would be optimized for either two or
three dimensions.
Solid Objects
Suppose we wish to determine whether a solid n-dimensional box, B,
intersects a solid n-dimensional sphere with center C and radius r. Here
end;
Figure 1. An algorithm for intersecting a solid n-dimensional box with a solid n–dimen-
sional sphere.
Hollow Objects
If we wish to make either or both of the objects hollow and test only their
surfaces, we can do so by regarding total inclusion of one object inside
the other as nonintersection. For instance, if we wish to test whether the
surface of the sphere intersects the solid box, we can add a test for
whether the box is entirely contained within the sphere and regard this as
nonintersection. This is shown in the algorithm of Fig. 2, in which we’ve
added the computation of the square distance from C to the farthest
point of B. If this value, denoted dmax , is less than r 2 , the entire box is
inside the sphere and there is no intersection.
The approach is different if we wish to disregard the interior of the box.
Here the constraints of Eq. 2 still hold but in order for the point P to be
on the boundary of the box we require the additional constraint that
Pi = Bi min or Pi = Bi max for some i. In the algorithm of Fig. 1 we see that
this holds unless Ci ∈(Bi ,Bi ) for all i. In this case we need to
min max
for i ← 1 . . . n do
a ← (C i – Bmin
i
)2
b ← (C i – Bmaxi
) 2;
d max ← d max + max(a, b);
[
if C i Ò Bmin
i ]
, Bmax
i
then d min ← d min + min(a,b);
endloop;
end;
Figure 2. An algorithm for intersecting a solid n-dimensional box with a hollow
n-dimensional sphere.
end;
Generalizing to Ellipsoids
We can easily generalize this idea to work with axis-aligned ellipsoids,
that is, ellipsoids that result from scaling a sphere differently along the
coordinate axes. We can specify such a ellipsoid in n-space by its center,
C[R n , and a “radius” for each axis, a 1 , . . . , a n . The point P ∈ R n
is on or in such an ellipsoid if and only if
is on or in such an ellipsoid if and only if
C1 − P1 + ⋅ ⋅ ⋅ + Cn − Pn ≤ 1.
2 2
(3)
α 1 α n
− B max
2
if C i > B max
then d min ← d min + Ci α
i ;
i i
endloop;
Figure 4. An algorithm for intersecting a solid n-dimensional box with a solid n-dimen-
sional axis-aligned ellipsoid.