Audio/AudioDevice: Improve GetListenerRotation

This commit is contained in:
Jérôme Leclercq
2022-03-17 13:34:47 +01:00
parent 6165b3a101
commit 01061380ee
6 changed files with 56 additions and 7 deletions

View File

@@ -705,7 +705,6 @@ namespace Nz
*
* \see Lerp, Slerp
*/
template<typename T>
Quaternion<T> Quaternion<T>::Lerp(const Quaternion& from, const Quaternion& to, T interpolation)
{
@@ -726,6 +725,33 @@ namespace Nz
return interpolated;
}
template<typename T>
Quaternion<T> Quaternion<T>::LookAt(const Vector3<T>& forward, const Vector3<T>& up)
{
// From https://gamedev.stackexchange.com/questions/53129/quaternion-look-at-with-up-vector
Vector3<T> forward_w(1, 0, 0);
Vector3<T> axis = Vector3<T>::CrossProduct(forward, forward_w);
RadianAngle<T> angle = std::acos(Vector3<T>::DotProduct(forward, forward_w));
Vector3<T> third = Vector3<T>::CrossProduct(axis, forward_w);
if (Vector3<T>::DotProduct(third, forward) < 0)
angle = -angle;
Quaternion<T> q1 = Quaternion(angle, axis);
Vector3<T> up_l = q1 * up;
Vector3<T> right = Vector3<T>::Normalize(Vector3<T>::CrossProduct(forward, up));
Vector3<T> up_w = Vector3<T>::Normalize(Vector3<T>::CrossProduct(right, forward));
Vector3<T> axis2 = Vector3<T>::CrossProduct(up_l, up_w);
RadianAngle<T> angle2 = std::acos(Vector3<T>::DotProduct(forward, forward_w));
Quaternion<T> q2 = Quaternion(angle2, axis2);
return q2 * q1;
}
/*!
* \brief Gives the normalized quaternion
* \return A normalized quaternion from the quat