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.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please copy the string wPWaRw to the field below:

Social Widgets powered by AB-WebLog.com.