From d1a104bba46d2918c2a521e255edee07ea2e5f3f Mon Sep 17 00:00:00 2001 From: Antonis Geralis <43617260+planetis-m@users.noreply.github.com> Date: Sat, 17 Dec 2022 13:13:40 +0200 Subject: [PATCH] Fix vector2angle (#2832) * Fix vector2angle * Fix ; * use acosf * need a break * add comments --- src/raymath.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/raymath.h b/src/raymath.h index 34db2d885..d3130750c 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -307,9 +307,15 @@ RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2) } // Calculate angle from two vectors +// Parameters need to be normalized RMAPI float Vector2Angle(Vector2 v1, Vector2 v2) { - float result = -acos(v1.x*v2.x + v1.y*v2.y); + 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); return result; }