summaryrefslogtreecommitdiffhomepage
path: root/src/rshapes.c
diff options
context:
space:
mode:
authorkai-z99 <[email protected]>2024-05-29 23:24:44 -0700
committerGitHub <[email protected]>2024-05-30 08:24:44 +0200
commit606cc1d897db0a2df201b1c5f7e98bda29d74c64 (patch)
treeee236c075fb1adfe8130595aabb28ab79aa2004b /src/rshapes.c
parentd7a8af144d066e28e821916ac10e66407e7bfa05 (diff)
downloadraylib-606cc1d897db0a2df201b1c5f7e98bda29d74c64.tar.gz
raylib-606cc1d897db0a2df201b1c5f7e98bda29d74c64.zip
[rshapes]Circle line collision function (#4018)
* inital function * working 1 * optimize * optimized dot product * simplify * cleanup * cleanup * cleanup * comment * var name change * epsilon
Diffstat (limited to 'src/rshapes.c')
-rw-r--r--src/rshapes.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/rshapes.c b/src/rshapes.c
index 80df64e2..cb85c1c5 100644
--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -2315,6 +2315,30 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol
return collision;
}
+// Check if circle collides with a line created betweeen two points [p1] and [p2]
+RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2)
+{
+ float dx = p1.x - p2.x;
+ float dy = p1.y - p2.y;
+
+ if ((fabsf(dx) + fabsf(dy)) <= FLT_EPSILON)
+ {
+ return CheckCollisionCircles(p1, 0, center, radius);
+ }
+
+ float lengthSQ = ((dx * dx) + (dy * dy));
+ float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (lengthSQ);
+
+ if (dotProduct > 1.0f) dotProduct = 1.0f;
+ else if (dotProduct < 0.0f) dotProduct = 0.0f;
+
+ float dx2 = (p1.x - (dotProduct * (dx))) - center.x;
+ float dy2 = (p1.y - (dotProduct * (dy))) - center.y;
+ float distanceSQ = ((dx2 * dx2) + (dy2 * dy2));
+
+ return (distanceSQ <= radius * radius);
+}
+
// Get collision rectangle for two rectangles collision
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
{