• 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
3
Question by Leigh · Feb 11, 2010 at 07:15 AM · rotationmath

Converting Matrix4x4 to Quaternion & Vector3

I'm attempting to convert a Matrix4x4 which is being currently being applied to a camera using camera.worldToCameraMatrix and works fine.

I'm able to get the position out with:

Vector4 newCameraPos = matrix.GetColumn(3);
Camera.main.transform.position = Vector3(newCameraPos.x,newCameraPos.y,newCameraPos.z);

But at the moment can't get the quaternion to come out. I've pulled some code from a matrix and quaternion FAQ I stumbled across and have partially implemented, but at the moment its not behaving.

Quaternion matrixToQuaternionx(Matrix4x4 m1){ float T = 1 + m1[0] + m1[5] + m1[10];

 Quaternion q = Quaternion.identity;

 if ( T > 0.00000001f ){
     float S = Mathf.Sqrt(T) * 2;
     q.x = ( m1[9] - m1[6] ) / S;
     q.y = ( m1[2] - m1[8] ) / S;
     q.z = ( m1[4] - m1[1] ) / S;
     q.w = 0.25f * S;
 }else{
     Debug.Log("more to calculate here");
 }

 return q;

}

If I turn off the camera.worldToCameraMatrix, and rely on the Vector and Quaternion I get out (ignoring cases where it gets to the 'more to calculate here') the position of the camera looks fine, but its rotation is way off.

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

2 Replies

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

Answer by runevision · Feb 11, 2010 at 11:08 AM

For getting a quaternion from a Matrix4x4, this function works:

public static Quaternion QuaternionFromMatrix(Matrix4x4 m) {
    // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
    Quaternion q = new Quaternion();
    q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] + m[1,1] + m[2,2] ) ) / 2; 
    q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] - m[1,1] - m[2,2] ) ) / 2; 
    q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] + m[1,1] - m[2,2] ) ) / 2; 
    q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] - m[1,1] + m[2,2] ) ) / 2; 
    q.x *= Mathf.Sign( q.x * ( m[2,1] - m[1,2] ) );
    q.y *= Mathf.Sign( q.y * ( m[0,2] - m[2,0] ) );
    q.z *= Mathf.Sign( q.z * ( m[1,0] - m[0,1] ) );
    return q;
}

For getting the position, GetColumn(3) works fine.

Comment
Add comment · Show 8 · 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 Leigh · Feb 14, 2010 at 11:29 PM 2
Share

Thanks!

It should be noted that if you're attempting to convert a valid worldToCamera$$anonymous$$atrix to a quaternion rotation you'll need to adjust for the reversed z on camera and call something like:

Camera.main.transform.rotation = Quaternion.QuaternionFrom$$anonymous$$atrix(matrixToSet.inverse * $$anonymous$$atrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1,1,-1)));

avatar image 3Duaun · Jan 30, 2011 at 06:12 PM 0
Share

such a great solution, thank you Rune!

avatar image roamcel · Sep 15, 2011 at 07:27 AM 0
Share

What about the inverse operation? Obtaining a 4x4 out of a quaternion. Is that possible?

avatar image JoryRFerrell · Aug 30, 2013 at 11:58 PM 0
Share

@roamcel (or anyone else reading this more realistically)

You can in fact get the reverse (4x4mat from a quat). The following is python code I wrote so it shouldn't be too hard to read and write in your preferred language:

 from math import *
 
 q = (0, 1.87, 3.22, 6.43)
 
 q2 = [0,0,0,0]
 
 q2[0] = q[0] / sqrt(q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2)
 q2[1] = q[1] / sqrt(q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2)
 q2[2] = q[2] / sqrt(q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2)
 q2[3] = q[3] / sqrt(q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2)
 
 print q2, "\n"
 
 quat_$$anonymous$$atrix = [
                [1 - 2*q2[2]**2 - 2*q2[3]**2, 2*q2[1]*q2[2] - 2*q2[3]*q2[0], 2*q2[1]*q2[3] + 2*q2[2]*q2[0]],
                [2*q2[1]*q2[2] + 2*q2[3]*q2[0], 1 - 2*q2[1]**2 - 2*q2[3]**2, 2*q2[2]*q2[3] - 2*q2[1]*q2[0]],
                [2*q2[1]*q2[3] - 2*q2[2]*q2[0], 2*q2[2]*q2[3] + 2*q2[1]*q2[0], 1 - 2*q2[1]**2 - 2*q2[2]**2]
               ]
 
 for i in quat_$$anonymous$$atrix:
     print i
avatar image numberkruncher · Aug 31, 2013 at 04:52 PM 0
Share

@roamcel @JoryRFerrell Unity provides a nice easy way:

 $$anonymous$$atrix4x4 m = $$anonymous$$atrix4x4.TRS(Vector3.zero, quaterion, Vector3.one);

As for matrix decomposition (the question subject) here is the approach which I use:

http://answers.unity3d.com/questions/402280/how-to-decompose-a-trs-matrix.html

Show more comments
avatar image
12

Answer by Leto · May 21, 2012 at 09:50 AM

Here's a faster and easier solution:

public static Quaternion QuaternionFromMatrix(Matrix4x4 m) { return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1)); }

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 peacefulshade · Jul 25, 2012 at 01:08 AM 5
Share

Actually I can confirm that the

"For getting a quaternion from a $$anonymous$$atrix4x4, this function works: ..."

Doesn't work, and the

"Here's a faster and easier solution: ..."

Works.

I'm making a voxel engine that performs all sorts of transforms and the first method produces: alt text

While the second función produces alt text

And the control test uses Graphics.Draw$$anonymous$$esh() with the matrix transform as a parameter

screen shot 2012-07-24 at 19.28.09 pm.png (63.7 kB)
screen shot 2012-07-24 at 19.28.53 pm.png (63.1 kB)
avatar image longqian peacefulshade · Jan 21, 2017 at 02:00 AM 0
Share

I agree with 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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lookat Direction by AddTorque? 0 Answers

touch input speed to camera rotate speed. scaling function needed 0 Answers

Rotate an object in a specific time 0 Answers

Quaternions local/world 1 Answer

Rotate a Vector3 direction 3 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