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 LK84 · Mar 04, 2017 at 09:31 PM · quaternioneuleranglesmatrix4x4.trs

When does a Quaternion become invalid?

So my code suddenly threw the following error in the OnDrawGizmos() method Quaternion To Matrix conversion failed because input Quaternion is invalid {0.000000, 0.819208, 0.000000, 0.573472} l=0.999972 caused by Matrix4x4.TRS. I narrowed the problem to the small test case by manually typing in the Quaternion values:

     void Start () {
         Quaternion quat = new Quaternion(0, 0.8192084f, 0, 0.5734719f);
         Debug.Log("old Quat: " + quat.eulerAngles);
         Vector3 euler = quat.eulerAngles;
         Quaternion quat2 = Quaternion.Euler(euler);
         Debug.Log("new Quat " + quat2.eulerAngles);
         Vector3 pos = Vector3.right;
         Vector3 scale = Vector3.one;
 
         Matrix4x4 mat2 = Matrix4x4.TRS(pos, quat2, scale);
         Matrix4x4 mat1 = Matrix4x4.TRS(pos, quat, scale);
     }

Now, what's interesting: The last line throws the error mentioned above, the line second to last doesn't !!! So apparently there is some rounding going on when converting the Quaternion to Euler Angles and back to Quaternion. Still I'd love to have some explanation when a Quaternion is valid and when not?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Mar 05, 2017 at 12:46 AM

Sure, your first quaternion isn't properly normalized. It has a length of "0.999971882048", though since float doesn't have that many digits it's rounded to "0.9999862". printing the eulerangles makes not much sense as they are calculated from the quaternion. If you want to compare the values, use

 Debug.Log("old Quat: " + quat.ToString("F10"));

This will print the quaternion values with 10 decimal places. The result is:

 old Quat: (0.0000000000, 0.8192084000, 0.0000000000, 0.5734719000)
 new Quat: (0.0000000000, 0.8192197000, 0.0000000000, 0.5734798000)

The new quaternion has a length of about "0.9999909369577". Represented as a float will round to "1.0"

edit
If you want to properly normalize your quaternion, you can use this extension method:

 public static class QuaternionExt
 {
     public static Quaternion GetNormalized(this Quaternion q)
     {
         float f = 1f / Mathf.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
         return new Quaternion(q.x*f, q.y*f, q.z*f, q.w*f);
     }
 }

With that class somewhere in your project you can simply do

 quat = quat.GetNormalized();

Running your test with the normalized quaternion doesn't create the error and both (quat and quat2) have the same values.

Comment
Add comment · Share
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

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

64 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

Related Questions

How to smoothly align an object to a surface, but only partially 2 Answers

Need help with Quaternions - Making a VR Combination Lock 1 Answer

Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers

How is the rotation of a Transform converted into a Vector3 in the inspector ? 2 Answers

How to Constrain Quaternion Rotations Over Time? 1 Answer


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