summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorTheSergey496 <[email protected]>2021-10-24 18:31:13 +0300
committerGitHub <[email protected]>2021-10-24 17:31:13 +0200
commit08441e8b2de0796ac6b12589c6abd4ac27f12b13 (patch)
tree837bf6bd1e6fc734898908c5d38248c8a1c78ff7 /src
parentd02310e8ce7b60b97ca8fab1ec65600057cd5d36 (diff)
downloadraylib-08441e8b2de0796ac6b12589c6abd4ac27f12b13.tar.gz
raylib-08441e8b2de0796ac6b12589c6abd4ac27f12b13.zip
When one of the colliding lines is vertical or horizontal, not all collisions were correctly detected due to floating point error. (#2081)
Diffstat (limited to 'src')
-rw-r--r--src/rshapes.c11
1 files 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 <math.h> // Required for: sinf(), asinf(), cosf(), acosf(), sqrtf(), fabsf()
+#include <float.h> // 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)
{