geometry
collision detection
computational geometry
3D graphics
sphere intersection

Determining if a sphere intersects an object or not

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A sphere intersection test is usually a distance problem, not a rendering problem. If you can compute the closest point on the target object to the sphere center, you can decide whether the sphere overlaps, touches, or misses the object.

The exact test depends on the kind of object you are checking against. A sphere versus plane, box, line segment, or triangle each has a different closest-point calculation, but they all use the same final idea: compare a squared distance with the squared radius.

Distance-Based Intersection Tests

The most reliable pattern is:

  1. Find the point on the object closest to the sphere center.
  2. Compute the squared distance from the center to that point.
  3. Report an intersection when distanceSquared <= radiusSquared.

Using squared distance avoids an unnecessary square root and is standard in physics engines and game loops.

For an axis-aligned bounding box, clamp each coordinate of the sphere center into the box range. The clamped point is the closest point on the box.

python
1from dataclasses import dataclass
2
3@dataclass
4class Vec3:
5    x: float
6    y: float
7    z: float
8
9@dataclass
10class AABB:
11    min: Vec3
12    max: Vec3
13
14
15def clamp(value: float, low: float, high: float) -> float:
16    return max(low, min(value, high))
17
18
19def sphere_intersects_aabb(center: Vec3, radius: float, box: AABB) -> bool:
20    closest = Vec3(
21        clamp(center.x, box.min.x, box.max.x),
22        clamp(center.y, box.min.y, box.max.y),
23        clamp(center.z, box.min.z, box.max.z),
24    )
25
26    dx = center.x - closest.x
27    dy = center.y - closest.y
28    dz = center.z - closest.z
29
30    distance_squared = dx * dx + dy * dy + dz * dz
31    return distance_squared <= radius * radius
32
33
34box = AABB(Vec3(0.0, 0.0, 0.0), Vec3(2.0, 2.0, 2.0))
35print(sphere_intersects_aabb(Vec3(3.0, 1.0, 1.0), 1.1, box))
36print(sphere_intersects_aabb(Vec3(4.0, 1.0, 1.0), 1.1, box))

The first call returns True because the sphere reaches the box face at x = 2. The second returns False because the center is too far away.

Common Object Types

For a plane, you do not need a closest point implementation if you already know the signed distance formula. A sphere intersects the plane when the absolute distance from the center to the plane is at most the radius.

python
1from math import sqrt
2
3
4def sphere_intersects_plane(center, radius, a, b, c, d):
5    numerator = abs(a * center.x + b * center.y + c * center.z + d)
6    denominator = sqrt(a * a + b * b + c * c)
7    distance = numerator / denominator
8    return distance <= radius

For a line segment or triangle, the idea is the same, but the closest point computation is more involved. In practice, engines often test the sphere against a broad-phase structure first, such as a bounding box hierarchy, then run a more expensive narrow-phase test only on likely candidates.

This is why “does a sphere intersect an object” is not a single universal formula. The sphere part stays simple; the object determines how you compute the nearest point.

Choosing a Practical Strategy

If the object is complex, do not test the sphere against every polygon immediately. Use a layered approach.

For example:

  • Sphere versus scene bounding box to reject far-away objects.
  • Sphere versus object bounding box to reject most candidates.
  • Sphere versus actual triangle mesh or analytic shape only when needed.

That pattern improves performance and keeps the code maintainable. It also makes debugging easier because you can inspect each stage separately.

If the object is moving, compute in a consistent coordinate space. Many bugs come from testing a sphere in world space against an object whose bounds are still in local space. Either transform the sphere into object space or transform the object representation into world space before running the test.

Common Pitfalls

A frequent mistake is using center-to-center distance even when the other object is not a sphere. That only works for sphere-versus-sphere tests. For a box, plane, or segment, you need a closest-point calculation.

Another common bug is comparing unsquared distance with squared radius, or the reverse. Pick one form and use it consistently. Squared values are usually better for performance and numerical stability.

Developers also often treat touching as “no intersection” by using < instead of <=. Whether tangency counts as a hit depends on your application, but collision systems usually include it.

Finally, avoid mixing transformed and untransformed geometry. If the sphere center is in world coordinates and the box is still stored in model coordinates, the result is meaningless even if the math is otherwise correct.

Summary

  • Sphere intersection tests reduce to finding the nearest point on the target object.
  • Compare squared distance to squared radius to avoid extra square roots.
  • Sphere-versus-box and sphere-versus-plane are cheap and common building blocks.
  • For complex meshes, use broad-phase culling before narrow-phase testing.
  • Most wrong answers come from coordinate-space mismatches or the wrong distance formula.

Course illustration
Course illustration

All Rights Reserved.