JOML-CI / JOML

A Java math library for OpenGL rendering calculations
MIT License
715 stars 102 forks source link

testSphereSphere divides by zero if used on two spheres that share a center #353

Open sovdeeth opened 1 month ago

sovdeeth commented 1 month ago

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;
    }