diff options
| author | Ray <[email protected]> | 2022-12-18 18:00:14 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2022-12-18 18:00:14 +0100 |
| commit | d7f7c94c4d44df89f6104dcda93eb330fc8534f3 (patch) | |
| tree | 1b70f6689f5c58bc4d5be3f499b95c21daefb1ca /src/raymath.h | |
| parent | 72b9f3c5def47fe6bf840466e464485878d93967 (diff) | |
| download | raylib-d7f7c94c4d44df89f6104dcda93eb330fc8534f3.tar.gz raylib-d7f7c94c4d44df89f6104dcda93eb330fc8534f3.zip | |
REVIEWED: `Vector2Angle()`
Diffstat (limited to 'src/raymath.h')
| -rw-r--r-- | src/raymath.h | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/raymath.h b/src/raymath.h index d3130750..ea3085a5 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -307,15 +307,23 @@ RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2) } // Calculate angle from two vectors -// Parameters need to be normalized +// NOTE: Parameters need to be normalized +// Current implementation should be aligned with glm::angle RMAPI float Vector2Angle(Vector2 v1, Vector2 v2) { - float dotProduct = v1.x*v2.x + v1.y*v2.y; // Dot product - - float t = dotProduct < -1 ? -1 : dotProduct; // Clamp - if (t > 1) t = 1; - - float result = acosf(t); + float result = 0.0f; + + float dot = v1.x*v2.x + v1.y*v2.y; // Dot product + + float dotClamp = (dot < -1.0f)? -1.0f : dot; // Clamp + if (dotClamp > 1.0f) dotClamp = 1.0f; + + result = acosf(dotClamp); + + // Alternative implementation, more costly + //float v1Length = sqrtf((v1.x*v1.x) + (v1.y*v1.y)); + //float v2Length = sqrtf((v2.x*v2.x) + (v2.y*v2.y)); + //float result = -acosf((v1.x*v2.x + v1.y*v2.y)/(v1Length*v2Length)); return result; } |
