REVIEWED: CheckCollisionPointPoly()

This commit is contained in:
Ray
2022-09-04 10:45:01 +02:00
parent aff98d7f2a
commit f4b4054de5
2 changed files with 7 additions and 8 deletions

View File

@@ -1632,18 +1632,17 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2
}
// Check if point is within a polygon described by array of vertices
bool CheckCollisionPointPolygon(Vector2 point, Vector2* vertices, int verticesCount)
// NOTE: Based on http://jeffreythompson.org/collision-detection/poly-point.php
bool CheckCollisionPointPoly(Vector2 point, Vector2 *points, int pointCount)
{
// http://jeffreythompson.org/collision-detection/poly-point.php
bool collision = false;
if (verticesCount > 2)
if (pointCount > 2)
{
for (int i = 0; i < verticesCount; i++)
for (int i = 0; i < pointCount; i++)
{
Vector2 vc = vertices[i];
Vector2 vn = vertices[i + 1];
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;