From c9d38fabe6da6f4759f9e8cd9560c378963fcb0a Mon Sep 17 00:00:00 2001 From: Matiis Date: Fri, 6 Jan 2017 23:07:03 +0100 Subject: [PATCH] Quaternion pitch/eulerAngles singularity handling Handling of singularity when using pitch or eulerAngles on quaternions like (0.5,0.5,-0.5,0.5) --- glm/gtc/quaternion.inl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/glm/gtc/quaternion.inl b/glm/gtc/quaternion.inl index 442cb4bf..dbdf504d 100644 --- a/glm/gtc/quaternion.inl +++ b/glm/gtc/quaternion.inl @@ -574,7 +574,13 @@ namespace detail template GLM_FUNC_QUALIFIER T pitch(tquat const & q) { - return T(atan(T(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z)); + //return T(atan(T(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z)); + const T y = T(2) * (q.y * q.z + q.w * q.x); + const T x = q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z; + if(y == T(0) && x == T(0)) //avoid atan2(0,0) - handle singularity - Matiis + return T(T(2)*atan(q.x,q.w)); + + return T(atan(y,x)); } template