• 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 thx2013 · Aug 27, 2018 at 08:50 AM · programmingscripting beginner

Standard assets MouseLook.cs script question

Hi there :)

I've recently started tinkering with Unity`s standard assets and one function in MouseLook.cs really interested me:

         Quaternion ClampRotationAroundXAxis(Quaternion q)
         {
             q.x /= q.w;
             q.y /= q.w;
             q.z /= q.w;
             q.w = 1.0f;
 
             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
 
             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
 
             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
 
             return q;
         }

These are the lines that I really do not understand:

             q.x /= q.w;
             q.y /= q.w;
             q.z /= q.w;
             q.w = 1.0f;

I presume that the rotation is being reset here since q.w is 1 (cos(0/2) == 1) , but what these divisions do: q.x /= q.w; q.x /= q.w; q.z /= q.w;. Can someone clarify this?

Comment
Add comment · Show 8
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 Harinezumi · Aug 27, 2018 at 08:50 AM 0
Share

This is a "normalization" of the rotation quaternion: the 4th element, w can be interpreted as a "weight" to the quaternion, so dividing all the elements with it you get a "normalized" quaternion (similar to how if you divide a Vector3 with its length ( magnitude) you get a vector that points in the same direction, but has a length of 1). You can also interpret the last line q.w = 1.0; as if you divided it with itself: q.w /= q.w;, but setting it to 1.0 is just faster and more precise.

This normalization is needed for the second part where you get the x rotation. If you didn't normalize, you wouldn't know what is the rotation along the x axis, because it can be multiplied by an arbitrary "weight".

avatar image Bunny83 Harinezumi · Aug 27, 2018 at 07:41 PM 1
Share

No, that's not normalizing. The resulting quaternion is no longer a unit quaternion. Unity automatically normalizes the quaternion when you assign a rotation. Normalization works the same way as it works for vectors. So you just divide all components by the "norm" or the quaternion (which is the same as the magnitude for vectors).


What they do here is essentially calculating "sin / cos" which is the tangent of half the angle. "w" is the real part while x,y and z are the imaginary vector part.


Though i'm not sure if this calculation actually works as it's always quite tricky to actually work directly with quaternions. It's actually quite similar to the axis - angle decomposition

avatar image Bunny83 Bunny83 · Aug 27, 2018 at 09:03 PM 1
Share

I've just did a test and this clamping does only work under certain circumstances. It does preserve the rotation when still inside the specified range. However when the clamping kicks in the rotation can get messed up. It works properly when the rotation only represents a rotation around the x axis. However if there's also a rotation around y the result is wrong. So this only work for a single rotation around the worldspace x axis

Show more comments
Show more comments
avatar image Sergio7888 · Aug 28, 2018 at 06:54 AM 1
Share

If you don't know Quaternion math "Dont touch it values directly". Quaternion

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

Multiple Cars not working 1 Answer

How to examine an object/examination system? 1 Answer

How can I make Stream Writer overwrite a txt file? 0 Answers

Need help with wall cover system(splinter cell like) 0 Answers

Custom script editor (good practices), 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