From 08441e8b2de0796ac6b12589c6abd4ac27f12b13 Mon Sep 17 00:00:00 2001 From: TheSergey496 <55157174+TheSergey496@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:31:13 +0300 Subject: [PATCH] When one of the colliding lines is vertical or horizontal, not all collisions were correctly detected due to floating point error. (#2081) --- src/rshapes.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index 76446ed8..f3f360a9 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -39,6 +39,7 @@ #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 #include // Required for: sinf(), asinf(), cosf(), acosf(), sqrtf(), fabsf() +#include // Required for: FLT_EPSILON //---------------------------------------------------------------------------------- // Defines and Macros @@ -1637,15 +1638,15 @@ bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, { const float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y); - if (div == 0.0f) return false; // WARNING: This check could not work due to float precision rounding issues... + if (abs(div) < FLT_EPSILON) return false; const float xi = ((startPos2.x - endPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div; const float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div; - if (xi < fminf(startPos1.x, endPos1.x) || xi > fmaxf(startPos1.x, endPos1.x)) return false; - if (xi < fminf(startPos2.x, endPos2.x) || xi > fmaxf(startPos2.x, endPos2.x)) return false; - if (yi < fminf(startPos1.y, endPos1.y) || yi > fmaxf(startPos1.y, endPos1.y)) return false; - if (yi < fminf(startPos2.y, endPos2.y) || yi > fmaxf(startPos2.y, endPos2.y)) return false; + if (abs(startPos1.x - endPos1.x) > FLT_EPSILON && (xi < fminf(startPos1.x, endPos1.x) || xi > fmaxf(startPos1.x, endPos1.x))) return false; + if (abs(startPos2.x - endPos2.x) > FLT_EPSILON && (xi < fminf(startPos2.x, endPos2.x) || xi > fmaxf(startPos2.x, endPos2.x))) return false; + if (abs(startPos1.y - endPos1.y) > FLT_EPSILON && (yi < fminf(startPos1.y, endPos1.y) || yi > fmaxf(startPos1.y, endPos1.y))) return false; + if (abs(startPos2.y - endPos2.y) > FLT_EPSILON && (yi < fminf(startPos2.y, endPos2.y) || yi > fmaxf(startPos2.y, endPos2.y))) return false; if (collisionPoint != 0) {