mirror of https://github.com/raysan5/raylib
REVIEWED: CheckCollision*() consistency
This commit is contained in:
parent
7158c80448
commit
f6180efd35
|
@ -1566,7 +1566,11 @@ bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
|
|||
// Check if point is inside circle
|
||||
bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius)
|
||||
{
|
||||
return CheckCollisionCircles(point, 0, center, radius);
|
||||
bool collision = false;
|
||||
|
||||
collision = CheckCollisionCircles(point, 0, center, radius);
|
||||
|
||||
return collision;
|
||||
}
|
||||
|
||||
// Check if point is inside a triangle defined by three points (p1, p2, p3)
|
||||
|
@ -1617,6 +1621,8 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa
|
|||
// NOTE: Reviewed version to take into account corner limit case
|
||||
bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
|
||||
{
|
||||
bool collision = false;
|
||||
|
||||
int recCenterX = (int)(rec.x + rec.width/2.0f);
|
||||
int recCenterY = (int)(rec.y + rec.height/2.0f);
|
||||
|
||||
|
@ -1632,37 +1638,45 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
|
|||
float cornerDistanceSq = (dx - rec.width/2.0f)*(dx - rec.width/2.0f) +
|
||||
(dy - rec.height/2.0f)*(dy - rec.height/2.0f);
|
||||
|
||||
return (cornerDistanceSq <= (radius*radius));
|
||||
collision = (cornerDistanceSq <= (radius*radius));
|
||||
|
||||
return collision;
|
||||
}
|
||||
|
||||
// Check the collision between two lines defined by two points each, returns collision point by reference
|
||||
bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint)
|
||||
{
|
||||
const float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y);
|
||||
bool collision = false;
|
||||
|
||||
if (fabsf(div) < FLT_EPSILON) return false;
|
||||
float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y);
|
||||
|
||||
const float xi = ((startPos2.x - endPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
|
||||
const float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
|
||||
|
||||
if (fabsf(startPos1.x - endPos1.x) > FLT_EPSILON && (xi < fminf(startPos1.x, endPos1.x) || xi > fmaxf(startPos1.x, endPos1.x))) return false;
|
||||
if (fabsf(startPos2.x - endPos2.x) > FLT_EPSILON && (xi < fminf(startPos2.x, endPos2.x) || xi > fmaxf(startPos2.x, endPos2.x))) return false;
|
||||
if (fabsf(startPos1.y - endPos1.y) > FLT_EPSILON && (yi < fminf(startPos1.y, endPos1.y) || yi > fmaxf(startPos1.y, endPos1.y))) return false;
|
||||
if (fabsf(startPos2.y - endPos2.y) > FLT_EPSILON && (yi < fminf(startPos2.y, endPos2.y) || yi > fmaxf(startPos2.y, endPos2.y))) return false;
|
||||
|
||||
if (collisionPoint != 0)
|
||||
if (fabsf(div) >= FLT_EPSILON)
|
||||
{
|
||||
collisionPoint->x = xi;
|
||||
collisionPoint->y = yi;
|
||||
collision = true;
|
||||
|
||||
float xi = ((startPos2.x - endPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
|
||||
float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
|
||||
|
||||
if (((fabsf(startPos1.x - endPos1.x) > FLT_EPSILON) && (xi < fminf(startPos1.x, endPos1.x) || (xi > fmaxf(startPos1.x, endPos1.x)))) ||
|
||||
((fabsf(startPos2.x - endPos2.x) > FLT_EPSILON) && (xi < fminf(startPos2.x, endPos2.x) || (xi > fmaxf(startPos2.x, endPos2.x)))) ||
|
||||
((fabsf(startPos1.y - endPos1.y) > FLT_EPSILON) && (yi < fminf(startPos1.y, endPos1.y) || (yi > fmaxf(startPos1.y, endPos1.y)))) ||
|
||||
((fabsf(startPos2.y - endPos2.y) > FLT_EPSILON) && (yi < fminf(startPos2.y, endPos2.y) || (yi > fmaxf(startPos2.y, endPos2.y))))) collision = false;
|
||||
|
||||
if (collision && (collisionPoint != NULL))
|
||||
{
|
||||
collisionPoint->x = xi;
|
||||
collisionPoint->y = yi;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return collision;
|
||||
}
|
||||
|
||||
// Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
|
||||
bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold)
|
||||
{
|
||||
bool collision = false;
|
||||
|
||||
float dxc = point.x - p1.x;
|
||||
float dyc = point.y - p1.y;
|
||||
float dxl = p2.x - p1.x;
|
||||
|
|
Loading…
Reference in New Issue