Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
1
Question by Glurth · May 08, 2017 at 06:50 PM · quaternionmatrix4x4

How to transform a Quaternion by a Matrix4x4?

Given a 4x4 matrix, and a Quaternion, what is the most efficient way to compute the matrix-transformed-Quaternion?

What I’m looking for is similar to, but not exactly the following. But I’d like to do it without all these extra operations, and no loss of data (in this example, rotation around the X axis (Vector3.right) could be lost.)

 Quaternion TransformQ(Matrix4x4 matrixTransformation, Quaternion inputQ)
         {
             Vector3 transformedRight = matrixTransformation.MultiplyPoint3x4(inputQ * Vector3.right);
             Quaternion finalQ = Quaternion.FromToRotation(Vector3.right, transformedRight);
             return finalQ;
         }

I would think this can be done by multiplying the Quaternion x,y,z,w components by the appropriate combination of matrix row/column values; I just don’t know the matrix math well enough.

Edit/Update: I finally found this question, which this one is similar to: http://answers.unity3d.com/questions/592099/how-to-muliply-a-quaternion-with-a-matrix4x4.html Alas, this answer yields a result that is a Matrix, and I'm looking for a quaternion.

I also found http://answers.unity3d.com/questions/402280/how-to-decompose-a-trs-matrix.html and am currently using the following base on it, but I'm not sure it's right: is it ok to just ignore the other elements of the transformation?

 Quaternion matrixRotation = Quaternion.LookRotation(
      matrix.GetColumn(2),
      matrix.GetColumn(1)     );
 Quaternion outputQ = matrixRotation * inputQ;
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

Answer by derBroV · Jul 27, 2020 at 12:51 PM

The quaternion has the character that you can multiply another quaternion and it's rotated by that. So you can inoutQ *= matrixRotaion.rotation. Correct me if I'm wrong.

Comment
Add comment · Show 2 · 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 Glurth · Jul 27, 2020 at 01:25 PM 0
Share

I think this is correct, https://docs.unity3d.com/ScriptReference/$$anonymous$$atrix4x4-rotation.html was added in version 2017.2, which was not yet released when this question was asked. Will test to confirm this works as expected, and will mark as correct answer at that point.

avatar image Bunny83 Glurth · Jul 27, 2020 at 01:49 PM 1
Share

The GetRotation method used by the rotation property most likely does something similar to what you have posted at the end of your question. Keep in $$anonymous$$d that you can not always extract a clear rotation from a matrix4x4 since it could represent a sheared space or a degenerated space. That's why rotation or the lossyscale extracted from a matrix might not be "correct".


The Transform class will most likely calculate the world space rotation seperately by just combining the quaternions of the hierarchy chain.


Finally be careful in which order you multiply quaternions since quaternion multiplication is not commutative. But I guess you know that ^^.

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

68 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

Related Questions

Acceleration and accelerometer calibration 0 Answers

Animation Position Shifting Player Controller Down 1 Answer

Questions about Rotations in unity 1 Answer

Touch based rotation goes wrong after some rotation applied to model 0 Answers

Combining Rotation = and Rotate() 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