dimforge / parry

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

SegmentsIntersection::Segment::first_loc1 is OnEdge([1.0, 0.0]) instead of OnVertex(0) #109

Open wlinna opened 1 year ago

wlinna commented 1 year ago

When using segments_intersection2d, I get misleading results. Instead of first_loc1 being OnVertex(0), I get OnEdge([1.0, 0.0]). While they represent the same point, the result is misleading since the doc comment of OnEdge says The point lies on the segment interior., and OnVertex would be more precise. One should also note that OnEdge([1.0, 0.0]) != OnVertex(0).

When I run the following code:

use rapier2d::prelude::*;

let seg1 = Segment::new(point![10.0, 0.0], point![10.0, 10.0]);
let seg2 = Segment::new(point![10.0, 0.0], point![10.0, 10.0]);

let intersection = rapier2d::parry::utils::segments_intersection2d(&seg1.a, &seg1.b, &seg2.a, &seg2.b, 0.0).unwrap();

let rapier2d::parry::utils::SegmentsIntersection::Segment { first_loc1, first_loc2, second_loc1, second_loc2 } = intersection else {
    unreachable!("The intersection should be a Segment intersection!");
};

dbg!(first_loc1);
dbg!(first_loc2);
dbg!(second_loc1);
dbg!(second_loc2);

The output is

first_loc1 = OnEdge([1.0, 0.0])
first_loc2 = OnVertex(0)
second_loc1 = OnEdge([0.0, 1.0,])
second_loc2 = OnVertex(1)

The output should be:

first_loc1 = OnVertex(0)
first_loc2 = OnVertex(0)
second_loc1 = OnVertex(1)
second_loc2 = OnVertex(1)

rustc: 1.65 parry2d: tested on 0.10 and 0.11.1

wlinna commented 5 months ago

I tested this on parry2d 0.14, and the bug still exists