In the following code, if the centers are equal, distSquared will be 0 and h becomes either infinity, -infinity, or NaN depending on the radii. The infinity cases resolve on the next line when multiplied by 0, but the NaN case does not (when the two spheres are identical).
Workaround is to compare equality before running the test, but I'd prefer just being able to test any two spheres without worry.
public static boolean testSphereSphere(
double aX, double aY, double aZ, double radiusSquaredA,
double bX, double bY, double bZ, double radiusSquaredB) {
double dX = bX - aX, dY = bY - aY, dZ = bZ - aZ;
double distSquared = dX * dX + dY * dY + dZ * dZ;
double h = 0.5 + (radiusSquaredA - radiusSquaredB) / (2.0 * distSquared);
double r_i = radiusSquaredA - h * h * distSquared;
return r_i >= 0.0;
}
In the following code, if the centers are equal, distSquared will be 0 and h becomes either infinity, -infinity, or NaN depending on the radii. The infinity cases resolve on the next line when multiplied by 0, but the NaN case does not (when the two spheres are identical).
Workaround is to compare equality before running the test, but I'd prefer just being able to test any two spheres without worry.