- Home /
Quaternion to Euler conversion singularities
HI All,
I am trying to write a code allowing me to extract rotation data from Unity to use in other applications. In this case, Rhino/Grasshopper. The issue is, it requires translation of Quaternion rotations into Euler angles. Every conversion I tried to far creates singularities / gumball locks which I can't seem to resolve.
Being not satisfied with the results of the in-built Unity conversions I have found this C# solution (http://stackoverflow.com/questions/12088610/conversion-between-euler-quaternion-like-in-unity3d-engine). While it solves some issues and makes Euler angles cleaner, some singularities still persist.
The script below already runs two singularity checks. I suspect it needs more but I am out of my depth here so any advise/suggestions would be greatly appreciated! Attached image is an example of the issue.
public static Vector3 FromQ2(Quaternion q1)
{
float sqw = q1.w * q1.w;
float sqx = q1.x * q1.x;
float sqy = q1.y * q1.y;
float sqz = q1.z * q1.z;
float unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
float test = q1.x * q1.w - q1.y * q1.z;
Vector3 v;
if (test > 0.4995f * unit)
{ // singularity at north pole
v.y = 2f * Mathf.Atan2(q1.y, q1.x);
v.x = Mathf.PI / 2;
v.z = 0;
return NormalizeAngles(v * Mathf.Rad2Deg);
}
if (test < -0.4995f * unit)
{ // singularity at south pole
v.y = -2f * Mathf.Atan2(q1.y, q1.x);
v.x = -Mathf.PI / 2;
v.z = 0;
return NormalizeAngles(v * Mathf.Rad2Deg);
}
Quaternion q = new Quaternion(q1.w, q1.z, q1.x, q1.y);
v.z = (float)Math.Atan2(2f * q.x * q.y + 2f * q.z * q.w, 1 - 2f * (q.y * q.y + q.z * q.z)); // Roll
v.y = (float)Math.Atan2(2f * q.x * q.w + 2f * q.y * q.z, 1 - 2f * (q.z * q.z + q.w * q.w)); // Yaw
v.x = (float)Math.Asin(2f * (q.x * q.z - q.w * q.y)); // Pitch
return NormalizeAngles(v * Mathf.Rad2Deg);
}
static Vector3 NormalizeAngles(Vector3 angles)
{
angles.x = NormalizeAngle(angles.x);
angles.y = NormalizeAngle(angles.y);
angles.z = NormalizeAngle(angles.z);
return angles;
}
static float NormalizeAngle(float angle)
{
while (angle > 360)
angle -= 360;
while (angle < 0)
angle += 360;
return angle;
}
}
I don't see any "singularity" here. "180" and "-180" is the same rotation. If the object somehow shows different that that program most likely uses a different coordinate system.
Unity uses a left-handed-coordinate system and eulerangles are processed in the order:
Z->X->Y around world-space axis or Y->X->Z around local space axis. So if you imagine the object being aligned with the world axis in the beginning, it first rotates around the local up axis (which is initially the same as world up). Then around the local x-axis (which may no longer be aligned with world x) and finally around the local z axis. Or in pitch / yaw / roll it's Yaw -> $$anonymous$$ch ->Roll
So you first should check what coordinate system and order of rotation that other application uses before doing any conversion.
Euler angles are simply a "bad" representation of a rotation, First there are always two ways how you could represent the same rotation (given the same coordinate system and same order or rotations). Second eulerangles might suffer from gimbal lock. Third there is no general valid order in which the rotations "should" be performed.
Thanks for your reply!
After researching some more into differences between Unity and Rhino coordinate systems I have been able to fix the issue without any alterations to the code above. It was to do with me simply remapping Unity Z values to Rhino Y values without accounting for the fact Rhino uses right-handed coordinate system.
P.S. Can you convert your comment to an answer so I can close this question?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How make child object rotate inverse to its parents rotation ? 1 Answer
Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles 0 Answers
Quaternion.Slerp and Quaternion.Euler problem 1 Answer
Player rotation 0 Answers