Math: Add AngleBetween and RotateTowards for Vector3 and Quaternion

This commit is contained in:
SirLynix
2023-12-07 16:50:22 +01:00
parent 3fd696385d
commit f5fefc7b86
4 changed files with 45 additions and 20 deletions

View File

@@ -4,8 +4,6 @@
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Math/Config.hpp>
#include <Nazara/Math/EulerAngles.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <cstring>
#include <limits>
#include <sstream>
@@ -116,6 +114,13 @@ namespace Nz
{
}
template<typename T>
RadianAngle<T> Quaternion<T>::AngleBetween(const Quaternion& quat) const
{
T alpha = Vector3<T>::DotProduct(Vector3<T>(x, y, z), Vector3<T>(quat.x, quat.y, quat.z));
return std::acos(Nz::Clamp(alpha, T(-1.0), T(1.0)));
}
template<typename T>
constexpr bool Quaternion<T>::ApproxEqual(const Quaternion& quat, T maxDifference) const
{
@@ -571,6 +576,12 @@ namespace Nz
return z >= quat.z;
}
template<typename T>
RadianAngle<T> Quaternion<T>::AngleBetween(const Quaternion& lhs, const Quaternion& rhs)
{
return lhs.AngleBetween(rhs);
}
template<typename T>
constexpr bool Quaternion<T>::ApproxEqual(const Quaternion& lhs, const Quaternion& rhs, T maxDifference)
{
@@ -687,6 +698,16 @@ namespace Nz
}
}
template<typename T>
Quaternion<T> Quaternion<T>::RotateTowards(const Quaternion& from, const Quaternion& to, RadianAngle<T> maxRotation)
{
RadianAngle<T> rotationBetween = AngleBetween(from, to);
if (rotationBetween < maxRotation)
return to;
return Slerp(from, to, std::min(maxRotation.value / rotationBetween.value), 1.f);
}
template<typename T>
Quaternion<T> Quaternion<T>::Mirror(Quaternion quat, const Vector3<T>& axis)
{