Reviewed function: CheckCollisionCircleRec()
This commit is contained in:
parent
da28cff0f6
commit
3b45336929
1 changed files with 13 additions and 5 deletions
18
src/shapes.c
18
src/shapes.c
|
@ -391,16 +391,24 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check collision between circle and rectangle
|
// Check collision between circle and rectangle
|
||||||
|
// NOTE: Reviewed version to take into account corner limit case
|
||||||
bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
|
bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
|
||||||
{
|
{
|
||||||
bool collision = false;
|
int recCenterX = rec.x + rec.width/2;
|
||||||
|
int recCenterY = rec.y + rec.height/2;
|
||||||
|
|
||||||
|
float dx = abs(center.x - recCenterX);
|
||||||
|
float dy = abs(center.y - recCenterY);
|
||||||
|
|
||||||
float dx = fabs((rec.x + rec.width/2) - center.x);
|
if (dx > (rec.width/2 + radius)) { return false; }
|
||||||
float dy = fabs((rec.y + rec.height/2) - center.y);
|
if (dy > (rec.height/2 + radius)) { return false; }
|
||||||
|
|
||||||
if ((dx <= (rec.width/2 + radius)) && (dy <= (rec.height/2 + radius))) collision = true;
|
if (dx <= (rec.width/2)) { return true; }
|
||||||
|
if (dy <= (rec.height/2)) { return true; }
|
||||||
|
|
||||||
return collision;
|
float cornerDistanceSq = pow(dx - rec.width/2, 2) + pow(dy - rec.height/2, 2);
|
||||||
|
|
||||||
|
return (cornerDistanceSq <= (radius*radius));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get collision rectangle for two rectangles collision
|
// Get collision rectangle for two rectangles collision
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue