From dfdeb9b28474f117d05f7358df6f51311b8b9ae6 Mon Sep 17 00:00:00 2001 From: Daniel Scharrer Date: Wed, 24 Jun 2020 23:32:48 +0200 Subject: [PATCH] Fix singularity in quaternion to euler angle roll conversion --- 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 06f9f020..e1ef0321 100644 --- a/glm/gtc/quaternion.inl +++ b/glm/gtc/quaternion.inl @@ -15,7 +15,13 @@ namespace glm template GLM_FUNC_QUALIFIER T roll(qua const& q) { - return static_cast(atan(static_cast(2) * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z)); + T const y = static_cast(2) * (q.x * q.y + q.w * q.z); + T const x = q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z; + + if(all(equal(vec<2, T, Q>(x, y), vec<2, T, Q>(0), epsilon()))) //avoid atan2(0,0) - handle singularity - Matiis + return static_cast(0); + + return static_cast(atan(y, x)); } template