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
0
Question by xtron32 · Jul 17, 2014 at 09:19 AM · c#animationmatrixmatrix4x4

How to get the Quaternion Rotation from a Matrix4x4

Hello, I'm trying to create animations during run time. It's going pretty good so far, I just have a few issues with retrieving a quaternion rotation from a matrix4x4. I was able to get the position and scale perfectly by using:


private Vector3 GetPosition(Matrix4x4 matrix)
{
    return matrix.GetColumn(3);
}
private Vector3 GetScale(Matrix4x4 matrix)
{
    return new Vector3(matrix.GetColumn(0).magnitude, matrix.GetColumn(1).magnitude, matrix.GetColumn(2).magnitude);
}

However, trying to figure out a feasible get rotation function that is exactly the same as unity engines importer for matrices, is another story. I have tried the following, which seem to partially work, but cause a little bit of jitter between key frames:

Both of these work to some extent, but do not produce the same results as unitys importer.

private Quaternion GetRotation(Matrix4x4 matrix) { return Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)); }

and

public static Quaternion QuaternionFromMatrix(Matrix4x4 m) {

 Quaternion q = new Quaternion();
 float absQ2 = Mathf.Pow(determinant(m), (1 / 3));
 q.w = Mathf.Sqrt(Mathf.Max(0, absQ2 + 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;

}

public static float determinant(Matrix4x4 m) { float tmp; tmp = m.m03 m.m12 m.m21 m.m30 - m.m02 m.m13 m.m21 m.m30 - m.m03 m.m11 m.m22 m.m30 + m.m01 m.m13 m.m22 m.m30 + m.m02 m.m11 m.m23 m.m30 - m.m01 m.m12 m.m23 m.m30 - m.m03 m.m12 m.m20 m.m31 + m.m02 m.m13 m.m20 m.m31 + m.m03 m.m10 m.m22 m.m31 - m.m00 m.m13 m.m22 m.m31 - m.m02 m.m10 m.m23 m.m31 + m.m00 m.m12 m.m23 m.m31 + m.m03 m.m11 m.m20 m.m32 - m.m01 m.m13 m.m20 m.m32 - m.m03 m.m10 m.m21 m.m32 + m.m00 m.m13 m.m21 m.m32 + m.m01 m.m10 m.m23 m.m32 - m.m00 m.m11 m.m23 m.m32 - m.m02 m.m11 m.m20 m.m33 + m.m01 m.m12 m.m20 m.m33 + m.m02 m.m10 m.m21 m.m33 - m.m00 m.m12 m.m21 m.m33 - m.m01 m.m10 m.m22 m.m33 + m.m00 m.m11 m.m22 m.m33; return tmp; }

To sum it up, I just need to figure out what unity does to get a quaternion rotation from a matrix4x4 on import of keyframes from fbx files.

Thanks so much,

~Matthew.

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
0
Best Answer

Answer by xtron32 · Jul 23, 2014 at 11:24 PM

It turns out that you have to use EnsureQuaternionContinuity() on the animation clips after creating them in memory, which will then perfectly convert all rotations to how unity does on import of fbx/dae/etc animation files.

Thanks!

Comment
Add comment · 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
2

Answer by sethuraj · Jul 17, 2014 at 09:58 AM

You can get the quaternion rotation from 4x4 rotation matrix using this formula

QuaternionW= √(1 + M00 + M11 + M22) /2

QuaternionX = (M21 - M12)/(QuaternionW * 4)

QuaternionY = (M02 - M20)/(QuaternionW * 4)

QuaternionZ = (M10 - M01)/(QuaternionW * 4)

for more refer here

http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/

Comment
Add comment · Show 1 · 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 xtron32 · Jul 17, 2014 at 09:02 PM 1
Share

Hi, thank you for answering me so quickly. I have actually looked into it a bit further and found that the first method that I said didn't work, actually did. It's just that unity modifies some of the $$anonymous$$atrix4x4's when it imports fbx files, and I have no idea why it modifies them a bit. If i grab the inverse of bind pose on bones being imported in run time, and run the first function to get the quaternion it matches the same numbers in the animation curves, which is strange. If you have any input on why this is, please let me know.

Thanks :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Animation disturbing circular rotation 1 Answer

How to Serialize an AnimationState 0 Answers

Custom AsyncOperation to show LoadingImage for slowish XML conversion & email upload method in Unity? 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