From 75ade552e20b4d6cc46543862305304202a9d0d3 Mon Sep 17 00:00:00 2001 From: William Linna Date: Wed, 1 May 2024 13:58:59 +0300 Subject: [PATCH] fix incorrect non-solid point projection called by project_local_point_and_get_location (#195) * Fix d_abc and d_bc computation in project_local_point_and_get_location * Fix https://github.com/dimforge/parry/issues/194 --- crates/parry2d/tests/lib.rs | 1 + crates/parry2d/tests/query/mod.rs | 1 + crates/parry2d/tests/query/point_triangle.rs | 20 ++++++++++++++++++++ src/query/point/point_triangle.rs | 4 ++-- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 crates/parry2d/tests/query/mod.rs create mode 100644 crates/parry2d/tests/query/point_triangle.rs diff --git a/crates/parry2d/tests/lib.rs b/crates/parry2d/tests/lib.rs index 206d0874..4557202e 100644 --- a/crates/parry2d/tests/lib.rs +++ b/crates/parry2d/tests/lib.rs @@ -4,3 +4,4 @@ extern crate nalgebra as na; extern crate parry2d; mod geometry; +mod query; diff --git a/crates/parry2d/tests/query/mod.rs b/crates/parry2d/tests/query/mod.rs new file mode 100644 index 00000000..b06213c4 --- /dev/null +++ b/crates/parry2d/tests/query/mod.rs @@ -0,0 +1 @@ +mod point_triangle; diff --git a/crates/parry2d/tests/query/point_triangle.rs b/crates/parry2d/tests/query/point_triangle.rs new file mode 100644 index 00000000..75fde225 --- /dev/null +++ b/crates/parry2d/tests/query/point_triangle.rs @@ -0,0 +1,20 @@ +use parry2d::{math::Point, query::PointQuery, shape::Triangle}; + +#[test] +fn project_local_point_point_on_ab() { + 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); // Used to fail on 0.14 and earlier + let proj2 = tri2.project_local_point(&query_pt, false); + + assert_eq!(proj1.point, proj2.point); + assert_eq!(proj1.point, query_pt); +} diff --git a/src/query/point/point_triangle.rs b/src/query/point/point_triangle.rs index a37a0c8f..09895198 100644 --- a/src/query/point/point_triangle.rs +++ b/src/query/point/point_triangle.rs @@ -252,8 +252,8 @@ impl PointQueryWithLocation for Triangle { let bc = c - b; let d_ab = ap.norm_squared() - (ab.norm_squared() * v * v); - let d_ac = ap.norm_squared() - (ac.norm_squared() * u * u); - let d_bc = bp.norm_squared() - (bc.norm_squared() * w * w); + let d_ac = ap.norm_squared() - (ac.norm_squared() * w * w); + let d_bc = bp.norm_squared() - (bc.norm_squared() * u * u); let proj; let loc;