diff options
| author | Antonio Raúl <[email protected]> | 2024-01-22 10:35:55 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-01-22 11:35:55 +0100 |
| commit | ef92ced370815a47d6d417ce6394b3156dd4f528 (patch) | |
| tree | fe0328d8c8199167553f01f3fabb35d4cc570288 /src | |
| parent | 68c32a41845b5216cba8b589ab312f0ca870c7d2 (diff) | |
| download | raylib-ef92ced370815a47d6d417ce6394b3156dd4f528.tar.gz raylib-ef92ced370815a47d6d417ce6394b3156dd4f528.zip | |
fix CheckCollisionPointPoly (#3750)
Diffstat (limited to 'src')
| -rw-r--r-- | src/rshapes.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/rshapes.c b/src/rshapes.c index 8e6708bf..e4eb107b 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2196,21 +2196,21 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 // NOTE: Based on http://jeffreythompson.org/collision-detection/poly-point.php bool CheckCollisionPointPoly(Vector2 point, Vector2 *points, int pointCount) { - bool collision = false; + bool inside = false; if (pointCount > 2) { - for (int i = 0; i < pointCount - 1; i++) + for (int i = 0, j = pointCount - 1; i < pointCount; j = i++) { - Vector2 vc = points[i]; - Vector2 vn = points[i + 1]; - - if ((((vc.y >= point.y) && (vn.y < point.y)) || ((vc.y < point.y) && (vn.y >= point.y))) && - (point.x < ((vn.x - vc.x)*(point.y - vc.y)/(vn.y - vc.y) + vc.x))) collision = !collision; + if ((points[i].y > point.y) != (points[j].y > point.y) && + (point.x < (points[j].x - points[i].x) * (point.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)) + { + inside = !inside; + } } } - return collision; + return inside; } // Check collision between two rectangles |
