Update rshapes.c

Add a much more efficient GetCollisionRec implementation
This commit is contained in:
manuel5975p 2023-04-30 13:33:46 +02:00 committed by GitHub
parent 59596e4266
commit cb2239f2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1757,71 +1757,31 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol
} }
// Get collision rectangle for two rectangles collision // Get collision rectangle for two rectangles collision
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) #define MIN_MACRO(X,Y) ((X) < (Y) ? (X) : (Y)) //Define ugly macros to get both gcc and clang
{ #define MAX_MACRO(X,Y) ((X) > (Y) ? (X) : (Y)) //to reduce min and max to single instructions
Rectangle rec = { 0, 0, 0, 0 }; Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) {
Rectangle overlap;
if (CheckCollisionRecs(rec1, rec2)) float left = MAX_MACRO(rec1.x, rec2.x);
{ float right = MIN_MACRO(rec1.x + rec1.width, rec2.x + rec2.width);
float dxx = fabsf(rec1.x - rec2.x); float top = MAX_MACRO(rec1.y, rec2.y);
float dyy = fabsf(rec1.y - rec2.y); float bottom = MIN_MACRO(rec1.y + rec1.height, rec2.y + rec2.height);
if (left < right && top < bottom) {
if (rec1.x <= rec2.x) overlap.x = left;
{ overlap.y = top;
if (rec1.y <= rec2.y) overlap.width = right - left;
{ overlap.height = bottom - top;
rec.x = rec2.x; } else {
rec.y = rec2.y; overlap.x = 0;
rec.width = rec1.width - dxx; overlap.y = 0;
rec.height = rec1.height - dyy; overlap.width = 0;
} overlap.height = 0;
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) return overlap;
{
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;
}
}
return rec;
} }
#undef MIN_MACRO //Undefine macros again
#undef MAX_MACRO
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module specific Functions Definition // Module specific Functions Definition