From 134260ba529ab57c8d014164ffc905cd65aa4c27 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Sat, 11 Sep 2021 06:00:58 -0700 Subject: [PATCH] Added Vector*::Angle functions to C++ Directly ported the C# versions of the function. --- Source/Engine/Core/Math/Vector2.h | 14 ++++++++++++++ Source/Engine/Core/Math/Vector3.h | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Source/Engine/Core/Math/Vector2.h b/Source/Engine/Core/Math/Vector2.h index 11abd87b9..ea177a14a 100644 --- a/Source/Engine/Core/Math/Vector2.h +++ b/Source/Engine/Core/Math/Vector2.h @@ -614,6 +614,20 @@ public: /// The third triangle vertex. /// The triangle area. static float TriangleArea(const Vector2& v0, const Vector2& v1, const Vector2& v2); + + /// + /// Calculates the angle (in radians) between from and to. This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in radians). + static float Angle(const Vector2& from, const Vector2& to) + { + const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); + if (Math::Abs(dot) > (1.0f - ZeroTolerance)) + return dot > 0.0f ? 0.0f : PI; + return Math::Acos(dot); + } }; inline Vector2 operator+(float a, const Vector2& b) diff --git a/Source/Engine/Core/Math/Vector3.h b/Source/Engine/Core/Math/Vector3.h index 47dc6a593..1a83169d2 100644 --- a/Source/Engine/Core/Math/Vector3.h +++ b/Source/Engine/Core/Math/Vector3.h @@ -929,6 +929,19 @@ public: /// The triangle area. static float TriangleArea(const Vector3& v0, const Vector3& v1, const Vector3& v2); + /// + /// Calculates the angle (in radians) between from and to. This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in radians). + static float Angle(const Vector3& from, const Vector3& to) + { + const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); + if (Math::Abs(dot) > (1.0f - ZeroTolerance)) + return dot > 0.0f ? 0.0f : PI; + return Math::Acos(dot); + } }; inline Vector3 operator+(float a, const Vector3& b)