Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
1
Question by Advokate · Feb 13, 2011 at 06:48 AM · rotationquaternioncoordinates

Quaternion Rotation Order and Axis

I have a question regarding Quaternions and rotation.

I want to rotate an object 45 degrees on the y axis, then 45 degrees on the x axis. I wanted to achieve this via quaternions so I did this:

Quaternion rotateY = Quaternion.AngleAxis(90, Vector3.up);
Quaternion rotateX = Quaternion.AngleAxis(45, Vector3.right);
transform.rotation = rotateY * rotateX;

What I expected to see is this, basically a rotation of 90 degrees about the global y axis, followed by a rotation of 45 degrees around the global x axis:

alt text

However, what I do see is this: alt text

It seems like when I composed the rotations with quaternions, its applied to the local coordinate instead of the global. so the rotation around the X axis use the "rotated" y axis after the first rotation. What I'm confused about is, How does all this work? From what I have read, I should be able to think of quaternions as rotational matrices, so the first rotation should be a rotation about the global Y axis and the second rotation should be about the global X axis. I am puzzled to why the second rotation use the "rotated" coordinate system instead since the quaternion does not "know" about the rotated coordinate system.

Any explanation to how this all works will be greatly appreciated

EDIT: In response to the first answer by Jesse, I wrote a simple test case of decomposing a rotation into 2 matrices verses using a combined matrix via the quaternion but they give different results, see below for what I did.

Quaternion rotateY = Quaternion.AngleAxis(90, Vector3.up); Quaternion rotateX = Quaternion.AngleAxis(90, Vector3.right);

Vector3 scaleUnity = new Vector3(1,1,1); Matrix4x4 matRotY = Matrix4x4.TRS(Vector3.zero, rotateY, scaleUnity); Matrix4x4 matRotX = Matrix4x4.TRS(Vector3.zero, rotateX, scaleUnity); // matrix multiplication order, matA*MatB --> first apply MatB (rotateY) then matA (rotateX) Vector3 resSeparate = (matRotX*matRotY).MultiplyPoint(Vector3.forward);

// quaternion multiplication Rotating a point first with lhs(rotateY) and then with rhs(rotateX) Matrix4x4 matCombined = Matrix4x4.TRS(Vector3.zero, rotateY*rotateX, scaleUnity); Vector3 resCombined = matCombined.MultiplyPoint(Vector3.forward);

Debug.Log("separate is " + resSeparate + " combined" + resCombined); // prints separate is (1.0, 0.0, 0.0) combined(0.0, -1.0, 0.0)

Must be something I'm missing here.

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 Jesse Anders · Feb 13, 2011 at 07:56 AM

Firstly, what you describe doesn't have anything specifically to do with quaternions; it's just how rotations behave, and will be the same whether you use quaternions or (e.g.) matrices.

What you're seeing is how Euler-angle rotations work. Each rotation in the sequence modifies the coordinate basis (in general), meaning, for example, that the first rotation in the sequence will generally appear to occur about one of the object's local axes rather than a global axis.

If this isn't the desired behavior, you can instead modify the orientation incrementally from update to update (rather than building it 'from scratch' as in your example).

Comment
Add comment · Show 5 · 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 Advokate · Feb 13, 2011 at 10:18 AM 0
Share

Thanks for the response, I did some further exploring in code and updated my question.

avatar image Jesse Anders · Feb 13, 2011 at 10:42 AM 0
Share

The quaternion multiplication order should be the same as the matrix multiplication order in this case, so ins$$anonymous$$d of 'rotateY*rotateX', you'll want 'rotateX*rotateY'. (The Unity math library uses column vectors and 'standard' quaternion multiplication order, which means that the multiplication order should be the same for both quaternions and matrices.)

avatar image Advokate · Feb 13, 2011 at 10:57 AM 0
Share

Yes, switching it around gives the behavior which I expect. However, the unity documentation here: http://unity3d.com/support/documentation/ScriptReference/Quaternion-operator_multiply.html

implies that a quaternion operation of: lhs*rhs is equivalent to rotating by lhs first and then rhs rather than the other way round.

avatar image Jesse Anders · Feb 13, 2011 at 12:48 PM 0
Share

The documentation, alas, is not always correct :) Some math libraries do swap quaternion multiplication order around in order to match the vector notation convention used, but I'm 99% sure (I'd have to check to be 100% sure) that Unity uses standard quaternion multiplication order, which means that the quaternion product p*q rotates by q and then by p.

avatar image Advokate · Feb 13, 2011 at 07:02 PM 0
Share

Thank you! This really helps clear things up for me

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

No one has followed this question yet.

Related Questions

Detecting Opposite Rotations of two Pipes 1 Answer

Extrapolating Quaternion Rotation 2 Answers

Make Quaternion affected by float 1 Answer

Rotation of an object with quaternion 1 Answer

why can't my Rigidbody not rotate on slopes? 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