diff options
| author | raysan5 <[email protected]> | 2020-12-23 12:45:53 +0100 |
|---|---|---|
| committer | raysan5 <[email protected]> | 2020-12-23 12:45:53 +0100 |
| commit | b78ee13791d9b31b06ad63799e547e7f0a2f73ff (patch) | |
| tree | c3121691fd6033c2948d42bf96ab4e74faf2fe09 /src/shapes.c | |
| parent | 2bdb45f2804461c4e8b35faee998008b96becabe (diff) | |
| download | raylib-b78ee13791d9b31b06ad63799e547e7f0a2f73ff.tar.gz raylib-b78ee13791d9b31b06ad63799e547e7f0a2f73ff.zip | |
ADDED: CheckCollisionLines()
Removed function from raymath
Diffstat (limited to 'src/shapes.c')
| -rw-r--r-- | src/shapes.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/shapes.c b/src/shapes.c index f5447241..fad55e24 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -1471,6 +1471,30 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) return (cornerDistanceSq <= (radius*radius)); } +// Check the collision between two lines defined by two points each, returns collision point by reference +bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint) +{ + 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 precission rounding issues... + + const float xi = ((startPos2.x - startPos2.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 (collisionPoint != NULL) + { + collisionPoint->x = xi; + collisionPoint->y = yi; + } + + return true; +} + // Get collision rectangle for two rectangles collision Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) { |
