GetCollisionV

This commit is contained in:
Dane Madsen 2025-12-29 17:38:45 +10:00
parent 8f8346048c
commit b67bab1f91
2 changed files with 31 additions and 0 deletions

View File

@ -1327,6 +1327,7 @@ RLAPI bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int th
RLAPI bool CheckCollisionPointPoly(Vector2 point, const Vector2 *points, int pointCount); // Check if point is within a polygon described by array of vertices
RLAPI bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint); // Check the collision between two lines defined by two points each, returns collision point by reference
RLAPI Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2); // Get collision rectangle for two rectangles collision
RLAPI Vector2 GetCollisionV(Rectangle rec1, Rectangle rec2); // Get collision vector for two rectangles collision
//------------------------------------------------------------------------------------
// Texture Loading and Drawing Functions (Module: textures)

View File

@ -2463,6 +2463,36 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
return overlap;
}
// Get collision vector for two rectangles collision
Vector2 GetCollisionV(Rectangle rec1, Rectangle rec2)
{
Rectangle inter = GetCollisionRec(rec1, rec2);
Vector2 result = { 0.0f, 0.0f };
if (inter.width <= 0.0f || inter.height <= 0.0f)
return result;
float ax = rec1.x + rec1.width * 0.5f;
float ay = rec1.y + rec1.height * 0.5f;
float bx = rec2.x + rec2.width * 0.5f;
float by = rec2.y + rec2.height * 0.5f;
float overlapX = inter.width;
float overlapY = inter.height;
float signX = (ax < bx) ? 1.0f : -1.0f;
float signY = (ay < by) ? 1.0f : -1.0f;
if (overlapX < overlapY) {
result.x = signX * overlapX;
} else {
result.y = signY * overlapY;
}
return result;
}
//----------------------------------------------------------------------------------
// Module Internal Functions Definition
//----------------------------------------------------------------------------------