From b67bab1f912928604ec07a712942e8a746964578 Mon Sep 17 00:00:00 2001 From: Dane Madsen Date: Mon, 29 Dec 2025 17:38:45 +1000 Subject: [PATCH] GetCollisionV --- src/raylib.h | 1 + src/rshapes.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index ba80e40c7..9b87bdd96 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -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) diff --git a/src/rshapes.c b/src/rshapes.c index 528a362d5..bb7d74302 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -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 //----------------------------------------------------------------------------------