optimized dot product
This commit is contained in:
parent
ba934b16c1
commit
6ad402a39d
|
|
@ -2317,11 +2317,6 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol
|
||||||
|
|
||||||
RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2)
|
RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2)
|
||||||
{
|
{
|
||||||
//test if end points are in circle
|
|
||||||
bool inside1 = CheckCollisionPointCircle(p1, center, radius);
|
|
||||||
bool inside2 = CheckCollisionPointCircle(p2, center, radius);
|
|
||||||
if (inside1 || inside2) return true;
|
|
||||||
|
|
||||||
// get length of line
|
// get length of line
|
||||||
float dx = p1.x - p2.x;
|
float dx = p1.x - p2.x;
|
||||||
float dy = p1.y - p2.y;
|
float dy = p1.y - p2.y;
|
||||||
|
|
@ -2330,16 +2325,20 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve
|
||||||
// dot product
|
// dot product
|
||||||
float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr);
|
float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr);
|
||||||
|
|
||||||
|
if (dotProduct > 1.0f)
|
||||||
|
{
|
||||||
|
dotProduct = 1.0f;
|
||||||
|
}
|
||||||
|
else if (dotProduct < 0.0f)
|
||||||
|
{
|
||||||
|
dotProduct = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
// closest point on line
|
// closest point on line
|
||||||
float closeX = p1.x + (dotProduct * (p2.x - p1.x));
|
float closeX = p1.x + (dotProduct * (p2.x - p1.x));
|
||||||
float closeY = p1.y + (dotProduct * (p2.y - p1.y));
|
float closeY = p1.y + (dotProduct * (p2.y - p1.y));
|
||||||
|
|
||||||
Vector2 close = { closeX, closeY };
|
Vector2 close = { closeX, closeY };
|
||||||
|
|
||||||
//is the point on the segment?
|
|
||||||
bool onSegment = CheckCollisionPointLine(close, p1, p2, 1);
|
|
||||||
if (!onSegment) return false;
|
|
||||||
|
|
||||||
//distnce from circle to closest point
|
//distnce from circle to closest point
|
||||||
|
|
||||||
dx = closeX - center.x;
|
dx = closeX - center.x;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user