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 /
avatar image
0
Question by Estecka · Nov 25, 2017 at 11:50 AM · rotationmatrix4x4transformation

Are these rotation matrices right ?

I've been trying to learn how transformation matrices work.

When it comes to rotation matrices, I actually found two opposed examples: one would use sin() and -sin() in some places, while the other swaps them around. In the end, are these rotation matrices right to use in Unity ?

In order: X Y & Z axis alt text

formula.jpg (42.8 kB)
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
6
Best Answer

Answer by Bunny83 · Nov 25, 2017 at 06:32 PM

The X and Y matrices seems to be correct however your Z matrix is transposed. So it rotates in the wrong direction.


Just test it in your head by multiplying a vector with only the first matrix. Take the forward vector (0,0,1) for example. If you rotate by "+90°" that means "sin" will be "1". So if you look at the z column we have a "-sin" in the y-row. So our incoming "1" z-value becomes a "-1" in the y component. This is correct. If you rotate in Unity by "+90" on the x axis you rotate downward.


A similar test can be done for the other two matrices. You will notice that the last one rotates in the wrong direction.


However what is actually wrong in your overall setup is your multiplication order. The order in which you combine matrices matters. Unity generally uses the order "YXZ" in local space which is the same as "ZXY" in worldspace. We do actually rotating in worldspace. Furthermore you have to keep in mind that when combining matrices they are executed from right to left. So since we want a worldspace order of "ZXY" you have to multiply them R = Y X Z


edit


I quickly created those helper functions:

 public static Matrix4x4 RotateX(float aAngleRad)
 {
     Matrix4x4 m = Matrix4x4.identity;     //  1   0   0   0 
     m.m11 = m.m22 = Mathf.Cos(aAngleRad); //  0  cos -sin 0
     m.m21 = Mathf.Sin(aAngleRad);         //  0  sin  cos 0
     m.m12 = -m.m21;                       //  0   0   0   1
     return m;
 }
 public static Matrix4x4 RotateY(float aAngleRad)
 {
     Matrix4x4 m = Matrix4x4.identity;     // cos  0  sin  0
     m.m00 = m.m22 = Mathf.Cos(aAngleRad); //  0   1   0   0
     m.m02 = Mathf.Sin(aAngleRad);         //-sin  0  cos  0
     m.m20 = -m.m02;                       //  0   0   0   1
     return m;
 }
 public static Matrix4x4 RotateZ(float aAngleRad)
 {
     Matrix4x4 m = Matrix4x4.identity;     // cos -sin 0   0
     m.m00 = m.m11 = Mathf.Cos(aAngleRad); // sin  cos 0   0
     m.m10 = Mathf.Sin(aAngleRad);         //  0   0   1   0
     m.m01 = -m.m10;                       //  0   0   0   1
     return m;
 }
 public static Matrix4x4 Rotate(Vector3 aEulerAngles)
 {
     var rad = aEulerAngles * Mathf.Deg2Rad;
     return RotateY(rad.y) * RotateX(rad.x) * RotateZ(rad.z);
 }

 public static Matrix4x4 Scale(Vector3 aScale)
 {
     var m = Matrix4x4.identity; //  sx   0   0   0
     m.m00 = aScale.x;           //   0  sy   0   0
     m.m11 = aScale.y;           //   0   0  sz   0
     m.m22 = aScale.z;           //   0   0   0   1
     return m;
 }
 public static Matrix4x4 Translate(Vector3 aPosition)
 {
     var m = Matrix4x4.identity; // 1   0   0   x
     m.m03 = aPosition.x;        // 0   1   0   y
     m.m13 = aPosition.y;        // 0   0   1   z
     m.m23 = aPosition.z;        // 0   0   0   1
     return m;
 }
 public static Matrix4x4 TRS(Vector3 aPos, Vector3 aEuler, Vector3 aScale)
 {
     return Translate(aPos) * Rotate(aEuler) * Scale(aScale);
 }


Those resemble Unity's euler rotation. So the following two matrices (m1 and m2) would be identical:

     var pos = new Vector3(2, 5, -7);
     var euler = new Vector3(20, 45, 10);
     var scale = new Vector3(20, 15, 5);
     var m1 = Matrix4x4.TRS(pos, Quaternion.Euler(euler), scale);
     var m2 = TRS(pos, euler, scale);
Comment
Add comment · Show 4 · 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 EricVoll · Oct 18, 2019 at 02:19 PM 1
Share

Aren't you missing a "1" in RotateZ at (2,2) (0 based matrix index)?

avatar image Bunny83 EricVoll · Oct 18, 2019 at 04:55 PM 0
Share

Ahh, yes ^^ Even the comments were just as reference they should be correct of course. Since we start with $$anonymous$$atrix4x4.identity the main diagonal is all 1's. Good spot anyways. I'll fix it.

avatar image EricVoll Bunny83 · Oct 19, 2019 at 09:24 AM 1
Share

I just saw that this post is over 2 years old but well - one typo less in this universe ;)

avatar image Bunny83 · Oct 19, 2019 at 04:18 PM 1
Share

Just for reference, Here's my matrix crash course which explains some basics and focus on the projection matrix

avatar image
1

Answer by jsabandal1 · Nov 25, 2017 at 12:39 PM

I've had a class about rotation matrices, and those are indeed rotation matrices. but using the actual formula to code for rotation is very tedious. we had to do it and it'll take you literally a lot of lines to code, plus there is more room for error. Using unity's method's would be advisable to use.

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 Estecka · Nov 25, 2017 at 12:58 PM 0
Share

Sorry, I didn't precise what was the concern exactly. I know those are rotation matrices, but I've seen examples where sin() and -sin() are swapped, and I wondered which versions were right in Unity.

I just found all those methods you mentionned that Unity provides. I should have started with looking for this...

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

103 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 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

Rotation Sequence 2 Answers

Convert tracking system position to unity world position/"Calibrate" tracking system 0 Answers

Rotate Vector3 array around a point 1 Answer

Get Accelerometer to rotate 360 degrees 2 Answers

The mathmatic inside transform.setparent and remain world position? How local trans value calculated? 0 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