Static Sphere vs Sphere

To detect whether two spheres are intersecting, you can simply calculate the distance of the sphere’s centers and if this is less than the combined radii of the spheres, the spheres are intersecting each other.

Normally, to calculate the distance between two points, the square root has to be taken. A small optimization you can apply is to instead square the radii as it involves a simple multiplication that can be computed much faster than the square root. The following C# code snippet demonstrates how this test can be implemented:

/// Tests whether two spheres intersect with each other
/// First sphere to test
/// Second sphere to test
/// True if the spheres intersect with each other
public static bool StaticTest(
  Vector3 firstCenter, double firstRadius,
  Vector3 secondCenter, double secondRadius
) {
  double distanceSquared = (firstCenter - secondCenter).SquaredLength;
  double summedRadii = firstRadius + secondRadius;

  // The spheres overlap if their combined radius is larger than the distance
  // between their centers
  return distanceSquared < (summedRadii * summedRadii);

Leave a Reply

Your email address will not be published. Required fields are marked *

Please copy the string wGuzPL to the field below:

This site uses Akismet to reduce spam. Learn how your comment data is processed.