summaryrefslogtreecommitdiffhomepage
path: root/src/shapes.c
diff options
context:
space:
mode:
authormkupiec1 <[email protected]>2021-03-31 16:50:05 +0200
committerGitHub <[email protected]>2021-03-31 16:50:05 +0200
commit3d1a05d588274536c35f1e667eb65f07e7e0d559 (patch)
tree6ad84035b65cf29b0616908cd2dd4f585befc641 /src/shapes.c
parent98a2e16d4d0da2aaa4b0cab6edcd1154e47999d4 (diff)
downloadraylib-3d1a05d588274536c35f1e667eb65f07e7e0d559.tar.gz
raylib-3d1a05d588274536c35f1e667eb65f07e7e0d559.zip
[shapes] CheckCollisionPointTriangle (fix), CheckCollisionPointLine (new routine) (#1695)
* CheckCollisionPointTriangle * New feature proposal to existing collision detection routines. It checks if point [point] belongs to line created between two points [p1] and [p2] with defined margin in pixels[threshold]. 1693
Diffstat (limited to 'src/shapes.c')
-rw-r--r--src/shapes.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/shapes.c b/src/shapes.c
index 89f647d4..789a1f70 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -1468,7 +1468,7 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2
float gamma = 1.0f - alpha - beta;
- if ((alpha > 0) && (beta > 0) & (gamma > 0)) collision = true;
+ if ((alpha > 0) && (beta > 0) && (gamma > 0)) collision = true;
return collision;
}
@@ -1545,6 +1545,26 @@ bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2,
return true;
}
+// Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
+bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold)
+{
+ bool collision = false;
+ float dxc = point.x - p1.x;
+ float dyc = point.y - p1.y;
+ float dxl = p2.x - p1.x;
+ float dyl = p2.y - p1.y;
+ float cross = dxc*dyl - dyc*dxl;
+
+ if (abs(cross) < threshold*fmaxf(fabsf(dxl), fabsf(dyl)))
+ {
+ if (fabsf(dxl) >= fabsf(dyl)) collision = (dxl > 0)? ((p1.x <= point.x) && (point.x <= p2.x)) : ((p2.x <= point.x) && (point.x <= p1.x));
+ else
+ collision = (dyl > 0)? ((p1.y <= point.y) && (point.y <= p2.y)) : ((p2.y <= point.y) && (point.y <= p1.y));
+ }
+
+ return collision;
+}
+
// Get collision rectangle for two rectangles collision
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
{