summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormanuel5975p <[email protected]>2023-05-10 19:19:59 +0200
committerGitHub <[email protected]>2023-05-10 19:19:59 +0200
commitaf4b97a301c39ca8df628b1889b8f6acd393a0f5 (patch)
treeed35272631fa570d77301f701eeb3cf90200b5df
parent152262dbfca8904e0250662dcee3985f57d7f9a3 (diff)
downloadraylib-af4b97a301c39ca8df628b1889b8f6acd393a0f5.tar.gz
raylib-af4b97a301c39ca8df628b1889b8f6acd393a0f5.zip
Update GetCollisionRec (#3052)
* Update rshapes.c Add a much more efficient GetCollisionRec implementation * Update GetCollisionRec Replace macros with ternary operators
-rw-r--r--src/rshapes.c85
1 files changed, 22 insertions, 63 deletions
diff --git a/src/rshapes.c b/src/rshapes.c
index 45cf6ac6..f7546cdf 100644
--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -1756,70 +1756,29 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol
}
// Get collision rectangle for two rectangles collision
-Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
-{
- Rectangle rec = { 0, 0, 0, 0 };
-
- if (CheckCollisionRecs(rec1, rec2))
- {
- float dxx = fabsf(rec1.x - rec2.x);
- float dyy = fabsf(rec1.y - rec2.y);
-
- if (rec1.x <= rec2.x)
- {
- if (rec1.y <= rec2.y)
- {
- rec.x = rec2.x;
- rec.y = rec2.y;
- rec.width = rec1.width - dxx;
- rec.height = rec1.height - dyy;
- }
- else
- {
- rec.x = rec2.x;
- rec.y = rec1.y;
- rec.width = rec1.width - dxx;
- rec.height = rec2.height - dyy;
- }
- }
- else
- {
- if (rec1.y <= rec2.y)
- {
- rec.x = rec1.x;
- rec.y = rec2.y;
- rec.width = rec2.width - dxx;
- rec.height = rec1.height - dyy;
- }
- else
- {
- rec.x = rec1.x;
- rec.y = rec1.y;
- rec.width = rec2.width - dxx;
- rec.height = rec2.height - dyy;
- }
- }
-
- if (rec1.width > rec2.width)
- {
- if (rec.width >= rec2.width) rec.width = rec2.width;
- }
- else
- {
- if (rec.width >= rec1.width) rec.width = rec1.width;
- }
-
- if (rec1.height > rec2.height)
- {
- if (rec.height >= rec2.height) rec.height = rec2.height;
- }
- else
- {
- if (rec.height >= rec1.height) rec.height = rec1.height;
- }
+Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2){
+ Rectangle overlap;
+ 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 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);
}
-
- return rec;
+ else{
+ overlap.x = 0;
+ overlap.y = 0;
+ overlap.width = 0;
+ overlap.height = 0;
+ }
+ return overlap;
}
//----------------------------------------------------------------------------------