Fix vector2angle (#2832)

* Fix vector2angle

* Fix ;

* use acosf

* need a break

* add comments
This commit is contained in:
Antonis Geralis 2022-12-17 13:13:40 +02:00 committed by GitHub
parent c2b56c583a
commit d1a104bba4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -307,9 +307,15 @@ RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2)
} }
// Calculate angle from two vectors // Calculate angle from two vectors
// Parameters need to be normalized
RMAPI float Vector2Angle(Vector2 v1, Vector2 v2) 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; return result;
} }