# 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:

/// <summary>Tests whether two spheres intersect with each other</summary> /// <param name="firstSphere">First sphere to test</param> /// <param name="secondSphere">Second sphere to test</param> /// <returns>True if the spheres intersect with each other</returns> 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