Althought this problem might seem hard at first, it's really rather simple:
Just take the point closest to the sphere's center within the box (which is very simple, just take there sphere's center and limit it to the range of the box), then see how far this point is away from the sphere's center. If this distance is less than the radius of the sphere, the two objects are overlapping.
/// <summary>Test whether a sphere and an axis aligned box intersect each other</summary>
/// <param name="aabbMin">Minimum coordinate of the axis aligned box to test</param>
/// <param name="aabbMax">Maximum coordinate of the axis aligned box to test</param>
/// <param name="sphereCenter">Center to the sphere to test</param>
/// <param name="sphereRadius">Radius to the sphere to test</param>
/// <returns>True if the axis aligned box intersects with the sphere</returns>
/// <remarks>
/// Idea taken from the "Simple Intersection Tests for Games" article
/// on gamasutra by Gomez.
/// </remarks>
public static bool StaticTest(
Vector3 aabbMin, Vector3 aabbMax, Vector3 sphereCenter, double sphereRadius
) {
double totalDistance = 0;
// Accumulate the distance of the sphere's center on each axis
for(int i = 0; i < 3; ++i) {
// If the sphere's center is outside the aabb, we've got distance on this axis
if(sphereCenter[i] < aabbMin[i]) {
double borderDistance = aabbMin[i] - sphereCenter[i];
totalDistance += borderDistance * borderDistance;
} else if(sphereCenter[i] > aabbMax[i]) {
double borderDistance = sphereCenter[i] - aabbMax[i];
totalDistance += borderDistance * borderDistance;
}
// Otherwise the sphere's center is within the box on this axis, so the
// distance will be 0 and we do not need to accumulate anything at all
}
// If the distance to the box is lower than the sphere's radius, both are overlapping
return (totalDistance <= (sphereRadius * sphereRadius));
}
/// <param name="aabbMin">Minimum coordinate of the axis aligned box to test</param>
/// <param name="aabbMax">Maximum coordinate of the axis aligned box to test</param>
/// <param name="sphereCenter">Center to the sphere to test</param>
/// <param name="sphereRadius">Radius to the sphere to test</param>
/// <returns>True if the axis aligned box intersects with the sphere</returns>
/// <remarks>
/// Idea taken from the "Simple Intersection Tests for Games" article
/// on gamasutra by Gomez.
/// </remarks>
public static bool StaticTest(
Vector3 aabbMin, Vector3 aabbMax, Vector3 sphereCenter, double sphereRadius
) {
double totalDistance = 0;
// Accumulate the distance of the sphere's center on each axis
for(int i = 0; i < 3; ++i) {
// If the sphere's center is outside the aabb, we've got distance on this axis
if(sphereCenter[i] < aabbMin[i]) {
double borderDistance = aabbMin[i] - sphereCenter[i];
totalDistance += borderDistance * borderDistance;
} else if(sphereCenter[i] > aabbMax[i]) {
double borderDistance = sphereCenter[i] - aabbMax[i];
totalDistance += borderDistance * borderDistance;
}
// Otherwise the sphere's center is within the box on this axis, so the
// distance will be 0 and we do not need to accumulate anything at all
}
// If the distance to the box is lower than the sphere's radius, both are overlapping
return (totalDistance <= (sphereRadius * sphereRadius));
}