Skip to content

Floating-Point Precision Causes Unexpected Intersection Errors #5

@GuoJiechang

Description

@GuoJiechang

I encountered an issue when performing intersection tests with triangles where small floating-point differences lead to unexpected errors.

Triangle values to reproduce the error:

// Case that causes intersection error
__t1.a.set(0, 50.000000000000014, 50);
__t1.b.set(0, 49.999999999999986, -50);
__t1.c.set(100, 50.000000000000014, 50);
__t2.a.set(17, 50, -24.543842315673828);
__t2.b.set(18, 50, -23.144481658935547);
__t2.c.set(18, 49, -23.595157623291016);

Image

However, if I round the floating-point values to exact integers, the issue disappears:

// Case without error
__t1.a.set(0, 50, 50);
__t1.b.set(0, 50, -50);
__t1.c.set(100, 50, 50);
__t2.a.set(17, 50, -24.543842315673828);
__t2.b.set(18, 50, -23.144481658935547);
__t2.c.set(18, 49, -23.595157623291016);

Image

Expected Behavior:
Small floating-point differences should not cause the intersection result to be wrong if the triangles are effectively the same in real-world positioning.

Potential solution:
in src/utils.ts, function orient3D(), line 40. Round the determinate to an integer.
const det = Math.round(_matrix4.determinant());

But I'd like to know if it is the best solution and if there are any potential issues.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions