New feature proposal to existing collision detection routines.

It checks if point [point] belongs to line created between two points [p1] and [p2] with defined margin in pixels[threshold].
1693
This commit is contained in:
mkupiec1 2021-03-31 16:24:56 +02:00
parent 68688e455e
commit 39b3af232a

View File

@ -1545,6 +1545,26 @@ bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2,
return true; return true;
} }
// 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;
float dyl = p2.y - p1.y;
float cross = dxc*dyl - dyc*dxl;
if (abs(cross) < threshold*fmaxf(fabsf(dxl), fabsf(dyl)))
{
if (fabsf(dxl) >= fabsf(dyl)) collision = (dxl > 0)? ((p1.x <= point.x) && (point.x <= p2.x)) : ((p2.x <= point.x) && (point.x <= p1.x));
else
collision = (dyl > 0)? ((p1.y <= point.y) && (point.y <= p2.y)) : ((p2.y <= point.y) && (point.y <= p1.y));
}
return collision;
}
// Get collision rectangle for two rectangles collision // Get collision rectangle for two rectangles collision
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
{ {