Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetCollisionRec #3052

Merged
merged 2 commits into from
May 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 22 additions & 63 deletions src/rshapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1757,70 +1757,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;
}

//----------------------------------------------------------------------------------
Expand Down