mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-06 03:18:14 +00:00
@@ -306,24 +306,33 @@ RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate angle from two vectors
|
// Calculate angle between two vectors
|
||||||
|
// NOTE: Angle is calculated from origin point (0, 0)
|
||||||
|
RMAPI float Vector2Angle(Vector2 v1, Vector2 v2)
|
||||||
|
{
|
||||||
|
float result = atan2f(v2.y - v1.y, v2.x - v1.x);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate angle defined by a two vectors line
|
||||||
// NOTE: Parameters need to be normalized
|
// NOTE: Parameters need to be normalized
|
||||||
// Current implementation should be aligned with glm::angle
|
// Current implementation should be aligned with glm::angle
|
||||||
RMAPI float Vector2Angle(Vector2 v1, Vector2 v2)
|
RMAPI float Vector2LineAngle(Vector2 start, Vector2 end)
|
||||||
{
|
{
|
||||||
float result = 0.0f;
|
float result = 0.0f;
|
||||||
|
|
||||||
float dot = v1.x*v2.x + v1.y*v2.y; // Dot product
|
float dot = start.x*end.x + start.y*end.y; // Dot product
|
||||||
|
|
||||||
float dotClamp = (dot < -1.0f)? -1.0f : dot; // Clamp
|
float dotClamp = (dot < -1.0f)? -1.0f : dot; // Clamp
|
||||||
if (dotClamp > 1.0f) dotClamp = 1.0f;
|
if (dotClamp > 1.0f) dotClamp = 1.0f;
|
||||||
|
|
||||||
result = acosf(dotClamp);
|
result = acosf(dotClamp);
|
||||||
|
|
||||||
// Alternative implementation, more costly
|
// Alternative implementation, more costly
|
||||||
//float v1Length = sqrtf((v1.x*v1.x) + (v1.y*v1.y));
|
//float v1Length = sqrtf((start.x*start.x) + (start.y*start.y));
|
||||||
//float v2Length = sqrtf((v2.x*v2.x) + (v2.y*v2.y));
|
//float v2Length = sqrtf((end.x*end.x) + (end.y*end.y));
|
||||||
//float result = -acosf((v1.x*v2.x + v1.y*v2.y)/(v1Length*v2Length));
|
//float result = -acosf((start.x*end.x + start.y*end.y)/(v1Length*v2Length));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user