summaryrefslogtreecommitdiffhomepage
path: root/src/rshapes.c
diff options
context:
space:
mode:
authorJacek <[email protected]>2022-09-04 10:39:03 +0200
committerGitHub <[email protected]>2022-09-04 10:39:03 +0200
commitaff98d7f2af466f4b066ac9a019b8ca1e450c5cd (patch)
treea5896496866fd21bb4fc0f6502ae2144534fe377 /src/rshapes.c
parent082920eb800d7d7612d500a4bbc46b21ff232424 (diff)
downloadraylib-aff98d7f2af466f4b066ac9a019b8ca1e450c5cd.tar.gz
raylib-aff98d7f2af466f4b066ac9a019b8ca1e450c5cd.zip
Check collision point polygon (#2685)
* Update raylib.h * CheckCollisionPointPolygon() * typo
Diffstat (limited to 'src/rshapes.c')
-rw-r--r--src/rshapes.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/rshapes.c b/src/rshapes.c
index 955f056d..8018bbde 100644
--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -1631,6 +1631,28 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2
return collision;
}
+// Check if point is within a polygon described by array of vertices
+bool CheckCollisionPointPolygon(Vector2 point, Vector2* vertices, int verticesCount)
+{
+ // http://jeffreythompson.org/collision-detection/poly-point.php
+
+ bool collision = false;
+
+ if (verticesCount > 2)
+ {
+ for (int i = 0; i < verticesCount; i++)
+ {
+ Vector2 vc = vertices[i];
+ Vector2 vn = vertices[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;
+ }
+ }
+
+ return collision;
+}
+
// Check collision between two rectangles
bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2)
{