Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions src/rshapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,41 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
return overlap;
}

// Get collision vector for two rectangles collision
Vector2 GetCollisionV(Rectangle rec1, Rectangle rec2)
{
Vector2 result;
result.x = 0.0f;
result.y = 0.0f;

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 dx = bx - ax;
float dy = by - ay;

float hx = (rec1.width * 0.5f) + (rec2.width * 0.5f);
float hy = (rec1.height * 0.5f) + (rec2.height * 0.5f);

float px = hx - fabsf(dx);
if (px <= 0.0f) return result;

float py = hy - fabsf(dy);
if (py <= 0.0f) return result;

if (px < py) {
result.x = (dx > 0.0f ? 1.0f : -1.0f) * px;
result.y = 0.0f;
} else {
result.x = 0.0f;
result.y = (dy > 0.0f ? 1.0f : -1.0f) * py;
}

return result;
}

//----------------------------------------------------------------------------------
// Module Internal Functions Definition
//----------------------------------------------------------------------------------
Expand Down
Loading