Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by FrojoS · Oct 06, 2012 at 12:07 PM · documentationquaternionsstandarddefinition

Representation of a Quaternion in Unity3d?

How is a quaternion defined/ represented in Unity3D? Is there any documentation on this?

I learned, that Unity uses a left handed coordinate system (COS). Also, it seems, that the rotation caused by the quaternions w value around the vector defined by the three values x,y and z is counterclockwise.

However, with my limited understanding of Quaternions, I would have expected said vector to behave differently. The most intuitive quaternion representation I can think of, is that the vector x,y,z would be a metric vector in Unity's left handed COS.

For instance, lets say my quaternion {x,y,z,w} was {1,0,0,0} I would expect my object to be not rotated at all. The x-axis would point towards the x-axis and there is not rotation around it (w = 0). Instead, Unity rotates my object around the x-axis by 180 degrees. If the quaternion was {0,1,0,0} I would expect the objects y-axis to be aligned with the worlds x-axis. Or, lets say my quaternion was {0,sqrt(1/2),0,sqrt(1/2)} I would expect my object to be rotated by 0.7 rounds, counterclockwise around the y-axis.

Again, my knowledge of quaternions is limited, but I have used rotation matrices for years. Currently, I have no other 3D visualisation software that I could use to see how their implementation works. I have however checked wolframalpha.com If you search there [1] for

 quaternion: 0 +1 i  +0 j +0 k

 quaternion: 0 +0 i  +1 j +0 k

or

 quaternion: sqrt(1/2) +0 i  +  sqrt(1/2) j +0 k

You get exactly the visualisations that I described above.

So how does Unity3D differ? I have tried to force Unity to behave like Wolframalpha but had only limited success so far.

 using UnityEngine;
 using System.Collections;
 
 public class quaternion : MonoBehaviour {
     
     
     public Quaternion q;
     public Vector3 a; //vector x,y,z (the complex value i,j,k in Wolframalpha
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 look = new Vector3(-a.x, -a.y, a.z);
         q = Quaternion.Euler(0,90,0)* Quaternion.LookRotation(look);
         transform.rotation = q;
     }
 }

Using the Quaternion.LookRotation function suggested elsewhere in this forum [2]. This works for simple cases like the second example {0,1,0,0} but it doesn't let me define the w value so the third example can't be implemented like this.

Any help is very much appreciated!

[1] http://www.wolframalpha.com/input/?i=quaternion%3A+0.3+%2B0+i++%2B1+j+%2B0+k You need to have their "CDF player" plugin installed and registered for the Wolframalpha Pro trial. Its annoying I know. I'm sure there are many well documented 3D tools that use quaternions that would do the same. Any recomendations? OpenGL + some wrapper?

[2] http://answers.unity3d.com/questions/14852/Convert-vector3-to-quaternion.html

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 FrojoS · Oct 06, 2012 at 12:56 PM 0
Share

Hi Fattie,

thanks a lot for your reply. I think I found my mistake and your hint

quats represent twists or rotations (they are like VERBS, not like NOUNS)

got me there. So, if I understand correct, the values x,y,z define the axis around which I rotate the object and w defines by how much I rotate. And, as I see now, the visualisation in Wolframalpha does exactly that, too.

However, it appears to me, that my idea would be an equivalent powerful representation. And a more intuitive one, at least for me. That is, x,y,z defines the orientation of the object with the last degree of freedom, which is by how much you rotate around that new orientation axis still being undefined. This could then be represented by w. I see if I can find the mathematical transformation between these two representations.

avatar image whydoidoit · Oct 06, 2012 at 05:04 PM 1
Share

I guess the point is: why do you want to know?

Quaternions are great tools to provide non-gimbal locked rotations. They are easy to combine to create rotations from logical parts. As @Fattie says they are a 4D representation of a rotation that uses complex numbers - but I've never found the need to fully understand the math, because it just hasn't been necessary and I can imagine it beco$$anonymous$$g necessary any time soon!

1 Reply

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

Answer by aldonaletto · Oct 06, 2012 at 07:54 PM

Good point! Unity seems to use a different representation for quaternions. A quaternion is a convenient (from the mathematicians point of view, of course) representation of a rotation around an arbitrary axis. Most papers say that the W component is the cosine of half the rotation angle, while the XYZ components are the unit vector representing the axis multiplied by the sine of (rotation angle/2):

 XYZ = sin(angle/2)*axis, W = cos(angle/2)

Unity also uses this notation, but the rotation angle seems to be mirrored about 180 degrees - this way, the zero rotation W should be 1 instead of 0 (the Quaternion.identity constant, which means zero rotation in Unity, is 0,0,0,1).
The functions Quaternion.AngleAxis and Quaternion.ToAngleAxis correctly convert angle/axis->quaternion and quaternion->angle/axis - at least in the Unity quaternion format.
All in all, I should agree with @Fattie: don't mess directly with quaternion components - use the Quaternion class functions instead.

EDITED: Dear God! We have been missing the obvious: W isn't the angle - it is the cosine of (rotation angle/2), and cos(0) is 1. The zero rotation quaternion should therefore be 0,0,0,1 - as it in fact is! Conclusion: Unity uses the standard quaternion format, were W is cos(angle/2) and XYZ is the rotation axis * sin(angle/2).

Comment
Add comment · Show 6 · 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 Fattie · Oct 07, 2012 at 06:19 AM 0
Share

Right. They should never, ever have mentioned the internals of Quaternions in the documentation. I mean they don't mention the internals of, say, rigidbody.

The documentation page explains incredibly clearly that you NEVER USE THE INTERNALS of a quaternion. But evidently users continually miss that and the identical message has to be repeated here! :)

avatar image whydoidoit · Oct 07, 2012 at 06:32 AM 1
Share

I do kind of think that it would have been wise to use different variable names than x,y,z which is bound to look like changing them should be changing the angle.

avatar image Fattie · Oct 07, 2012 at 06:42 AM 0
Share

that is an incredible insight - so true. $$anonymous$$an, that's sharp, you are so right. Excellent!!!!!!!!!!!!!

avatar image Fattie · Oct 07, 2012 at 06:43 AM 0
Share

You have easily won "pedagogical insight of the Quarter" award, damn

avatar image aldonaletto · Oct 07, 2012 at 11:13 AM 0
Share

@whydoidoit: yes, using XYZ is somewhat confusing - and Unity doesn't help showing localEulerAngles in the Inspector under the caption Rotation: almost everybody (me included) gets fooled at first, modifying transform.rotation components as if they were Euler angles.

Show more comments

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

13 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

Related Questions

Boxcast Rotaion/ Orientation Help 1 Answer

Get real compass direction 1 Answer

one class (File) with different definitions in different projects 1 Answer

Unable to add a tag to a GameObject in Unity 3.4 1 Answer

Where are events documented? 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