diff options
| author | Ray <[email protected]> | 2023-05-10 19:25:12 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2023-05-10 19:25:12 +0200 |
| commit | 452e3b494cd6bceaab290d0f5f23bff9c7887a1b (patch) | |
| tree | 1b452288a53487775d43851c4e191a4c320ce3fd /src/rshapes.c | |
| parent | af4b97a301c39ca8df628b1889b8f6acd393a0f5 (diff) | |
| download | raylib-452e3b494cd6bceaab290d0f5f23bff9c7887a1b.tar.gz raylib-452e3b494cd6bceaab290d0f5f23bff9c7887a1b.zip | |
REVIEWED: `GetCollisionRec()`
Diffstat (limited to 'src/rshapes.c')
| -rw-r--r-- | src/rshapes.c | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/rshapes.c b/src/rshapes.c index f7546cdf..27888642 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -1756,28 +1756,27 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol } // Get collision rectangle for two rectangles collision -Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2){ - Rectangle overlap; - float left = ((rec1.x) > (rec2.x) ? (rec1.x) : (rec2.x)); +Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) +{ + Rectangle overlap = { 0 }; + + float left = (rec1.x > rec2.x)? rec1.x : rec2.x; float right1 = rec1.x + rec1.width; float right2 = rec2.x + rec2.width; - float right = ((right1) < (right2) ? (right1) : (right2)); - float top = ((rec1.y) > (rec2.y) ? (rec1.y) : (rec2.y)); + float right = (right1 < right2)? right1 : right2; + float top = (rec1.y > rec2.y)? rec1.y : rec2.y; float bottom1 = rec1.y + rec1.height; float bottom2 = rec2.y + rec2.height; - float bottom = ((bottom1) < (bottom2) ? (bottom1) : (bottom2)); - if (left < right && top < bottom){ - overlap.x = (left); - overlap.y = (top); - overlap.width = (right - left ); - overlap.height = (bottom - top); - } - else{ - overlap.x = 0; - overlap.y = 0; - overlap.width = 0; - overlap.height = 0; + float bottom = (bottom1 < bottom2)? bottom1 : bottom2; + + if ((left < right) && (top < bottom)) + { + overlap.x = left; + overlap.y = top; + overlap.width = right - left; + overlap.height = bottom - top; } + return overlap; } |
