dimforge / parry

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

parry2d::utils::point_in_poly2d gives false negatives #89

Open kbjakex opened 2 years ago

kbjakex commented 2 years ago

The documentation says:

Tests if the given point is inside of a polygon with arbitrary orientation.

which sounds like it should work with any arbitrary polygon. However, in my application, I have non-convex polygons I tested against, and while there are no false positives, it leaves parts of the polygon out: Screenshot from 2022-08-25 14-37-10 (the tracing is inexact, I'm just spawning a dot whenever the mouse is detected to be inside the polygon)

Based on the output I'd guess it only works for convex polygons (although I don't understand the algorithm), in which case it would be nice to have a mention of this in the docs.

ZebTheWizard commented 2 years ago

I was having a similar problem where I was defining a list of points. Sometimes the method would return true but it mostly return false. Turns out my points were getting jumbled up. I think this only works if the points are defined in sequence clockwise or counter clockwise. If your points are "zig zagged" then it won't work.

If the method should handle concave and convex shapes, this makes sense because changing the sequence of points creates a different shape.

image

By arbitrary, I think they meant the ordering doesn't matter. For example, the following sequences all form the same polygon:

[[ 1, 1], [-1, 1], [-1,-1], [ 1,-1]]
[[-1, 1], [-1,-1], [ 1,-1], [ 1, 1]]
[[-1,-1], [ 1,-1], [ 1, 1], [-1, 1]]
[[ 1,-1], [ 1, 1], [-1, 1], [-1,-1]]
sebcrozet commented 2 years ago

This function only supports convex polylines, yeah. We should specify that in the docs. For non-convex (non-self-intersecting) polylines, you can use Polyline::project_local_point_assuming_solid_interior_ccw](https://docs.rs/parry2d/0.9.0/parry2d/shape/struct.Polyline.html#method.project_local_point_assuming_solid_interior_ccw). This method requires the polyline’s orientation to be counterclockwise.