Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sb526 · Mar 10, 2017 at 11:18 PM · c#rotationquaternionexporteuler

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.

alt text

 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;
     }
 }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 11, 2017 at 04:13 AM 1
Share

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.

avatar image sb526 · Mar 11, 2017 at 01:58 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

298 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges