[shapes] CheckCollisionPointTriangle (fix), CheckCollisionPointLine (new routine) (#1695)
* CheckCollisionPointTriangle * 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:
parent
98a2e16d4d
commit
3d1a05d588
1 changed files with 21 additions and 1 deletions
22
src/shapes.c
22
src/shapes.c
|
@ -1468,7 +1468,7 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2
|
||||||
|
|
||||||
float gamma = 1.0f - alpha - beta;
|
float gamma = 1.0f - alpha - beta;
|
||||||
|
|
||||||
if ((alpha > 0) && (beta > 0) & (gamma > 0)) collision = true;
|
if ((alpha > 0) && (beta > 0) && (gamma > 0)) collision = true;
|
||||||
|
|
||||||
return collision;
|
return collision;
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue