Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 qdeanc · Aug 21, 2021 at 04:35 AM · rotationquaternionvalueprecisionfloatingpoint

Quaternion.ToAngleAxis is Unprecise?

In the code below, I create a quaternion with a rotation of 0.02 degrees and print the quaternion's angle to the 5th decimal place.

 Quaternion q = Quaternion.Euler(0.02f, 0f, 0f);
 q.ToAngleAxis( out float qAngle, out Vector3 qAxis);
 
 print(qAngle.ToString("F5"));

But according to Unity, the angle of the quaternion is:

 0.00000

What's causing this lack of precision? I'm so lost :(

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
1
Best Answer

Answer by Bunny83 · Aug 21, 2021 at 07:32 AM

Because the angle is below the precision of 32 floating point numbers. The issue here is the way how rotations are represented as a quaternion. The quaternion that is created has the 4 values:

 float halfRadians = angleInDegree * Mathf.Deg2Rad / 2;
 x = 1 * Mathf.Sin(halfRadians);
 y = 0 * Mathf.Sin(halfRadians);
 z = 0 * Mathf.Sin(halfRadians);
 w = Mathf.Cos(halfRadians);

As you may know a quaternion is always represented as a "vector" (x,y,z) and the cosine of half the angle around that vector. The issue here is that 0.02 degree yields a cosine value of 0.99999998476912.... However this is beyond the precision that a float can represent, so the value is rounded to 1. Though 1 represents an angle of 0°. The vector part itself is scaled down by sine of half the angle and results in 0.0001745329 which can be represented with a float. That means the quaternion may still work within some error margin, but the "normal" way of retrieving the the axis / angle just doesn't work. Usually you would use "Acos" on the w value to retrieve half the angle which we would double. Though since that would yield a value of 0 that doesn't really help.


You can still recover the angle to a higher precision by manually recovering the angle from the vector part instead of the scaler part. However you have to handle the sign manually. Something like this should work:

     Vector3 axis = new Vector3(q.x, q.y, q.z);
     float len = axis.magnitude;
     float angle = Mathf.Asin(len) * Mathf.Rad2Deg * 2f * Mathf.Sign(q.w);
     axis *= 1f / len;

Note that this solution will have similar issues when the half the angle gets closer to 90° (so angle is close to 180°).

So you may want to use this solution only if the absolute value of "w" is very close to "1".

Comment
Add comment · Show 1 · 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
avatar image qdeanc · Aug 22, 2021 at 06:57 AM 0
Share

Thank you!!

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

166 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

Related Questions

Weird rotation precision 2 Answers

Convert Quaternion to Eulerangles 2 Answers

Rotating a 3D object relative to it's velocity, but only on a 2D plane 0 Answers

Help! Stuck with this rotation automation! I got this automation to work for position and localScale, but them damn Quaternions... 0 Answers

Rotation to go from one direction to another? 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