dimforge / parry

2D and 3D collision-detection library for Rust.
https://parry.rs
Apache License 2.0
528 stars 93 forks source link

project_local_point fails to find the correct projection when the point lies on a triangle edge #194

Closed wlinna closed 2 months ago

wlinna commented 2 months ago

In some cases when the point lies on a triangle edge project_local_point with solid = false project the point to some other edge. Interestingly enough, this seems to be winding order sensitive, because for tri2 the correct projection is computed.

Here's a code that reproduces the bug

fn main() {
    let verts = [Point::new(2.0, 1.0), Point::new(0.0, 1.0), Point::new(1.0, 0.0)];
    let tri1 = Triangle::new(verts[0], verts[1], verts[2]);
    let tri2 = Triangle::new(verts[2], verts[0], verts[1]);

    let query_pt = Point::new(1.4, 1.0);

    let proj1 = tri1.project_local_point(&query_pt, false); // FAILS
    let proj2 = tri2.project_local_point(&query_pt, false); // SUCCEEDS

    assert_eq!(proj1.point, proj2.point);

    // assertion `left == right` failed
    //  left: [1.7, 0.7]
    //  right: [1.4, 1.0]
}

Tested with parry2d 0.14 and parry2d 0.13.8