Skip to content

Commit

Permalink
Merge pull request #1019 from dscharrer/master
Browse files Browse the repository at this point in the history
Fix singularity in quaternion to euler angle roll conversion #1019
  • Loading branch information
christophe-lunarg authored Nov 21, 2020
2 parents e8f2e98 + dfdeb9b commit b033c73
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion glm/gtc/quaternion.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ namespace glm
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T roll(qua<T, Q> const& q)
{
return static_cast<T>(atan(static_cast<T>(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<T>(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<T>()))) //avoid atan2(0,0) - handle singularity - Matiis
return static_cast<T>(0);

return static_cast<T>(atan(y, x));
}

template<typename T, qualifier Q>
Expand Down

0 comments on commit b033c73

Please sign in to comment.