Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 torano · Aug 12, 2019 at 08:10 AM · scripting problemtransformmatrix4x4

How to manually calculate localToWorldMatrix/worldToLocalMatrix?

For learning purpose, I am trying to calculate transform.localToWorldMatrix and transform.worldToLocalMatrix manually. I found if a child object has a single parent, TRS matix made by Matrix4x4.TRS(), which I think it is just a transformation matrix storing the gameobject's position, rotation, and scale, is the same as localToWorldMatrix and its inverse matrix is the same as worldToLocalMatrix, as the link below says.

https://gamedev.stackexchange.com/questions/139003/calculate-vector3-global-point-projecting-it-in-local-space-using-unity-and-c

But If a child's parent has another parent, then that would not work well. So I figured out I need multiplication of each parent 's transformation matrix. Since Unity uses column major matrices, a world vertex position should be calculated as SuperParentMatrix * InternalParentMatrix * localVertex. I tried it as the code below, but it outputs a wrong value. What am I doing wrong? and How can I calculate it correctly?

 public class MultiParentTest : MonoBehaviour
 {
     [SerializeField]
     Transform[] parents;
 
     [SerializeField]
     GameObject child;
 
     // Start is called before the first frame update
     void Start()
     {
         LogPositionInfo();
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     [ContextMenu("Log Position Info")]
     void LogPositionInfo()
     {
         if (parents != null && parents.Length == 2 && child != null)
         {
             Matrix4x4 l2wMat = parents[0].GetTRSMatrix() * parents[1].GetTRSMatrix();   // why wrong?
             //Matrix4x4 l2wMat = parents[1].localToWorldMatrix;   // correct
 
             var pos = child.transform.position;
             var posInfo = $"Correct World Position:\n({pos.x}, {pos.y}, {pos.z})\n";
             pos = l2wMat.MultiplyPoint3x4(child.transform.localPosition);
             posInfo += $"localToWorld:\n({pos.x}, {pos.y}, {pos.z})\n";
 
             Matrix4x4 w2lMat = parents[1].GetTRSMatrix().inverse * parents[0].GetTRSMatrix().inverse;   // why wrong?
             //Matrix4x4 w2lMat = parents[1].worldToLocalMatrix;   // correct
 
             pos = child.transform.localPosition;
             posInfo += $"Correct Local Position:\n({pos.x}, {pos.y}, {pos.z})\n";
             pos = w2lMat.MultiplyPoint3x4(child.transform.position);
             posInfo += $"worldToLocal:\n({pos.x}, {pos.y}, {pos.z})\n";
 
 
 
             Debug.Log(posInfo);
         }
     }
 }
 
 static public class TransformExtension
 {
     static public Matrix4x4 GetTRSMatrix(this Transform transform)
     {
         return Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale);
     }
 }


Description of my scene that uses the code above: made 3 GameObject as parent0, parent1, and child. parent0 is a parent of parent1 and parent1 is a parent of child. set different transform values of them and put parent0 and parent1 into Transform[] parents as parent0 is the first element, and put child into GameObject child.

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

Answer by Bunny83 · Aug 12, 2019 at 12:29 PM

What you're doing makes no sense. You use the calculated worldspace parameters of the transform to create your matrix. This would already contain the full transformation, though it doesn't work as you can't rely on lossyScale. If there is any non-uniform scaling going on in the hierarchy it's impossible to specify a scale vector3. That's why it's named "lossy".


What you actually have to do is use the localPosition, localRotation and localScale to form a matrix which does not transform from local into worldspace but from local into parent space. Then chain all those matrices to get all the way up to worldspace. As you might know a Transform that has no parent implicitly has the worldspace as parent and position == localPosition, rotation == localRotation and lossyScale == localScale.


So your extension method need to do this:

 static public Matrix4x4 GetTRSMatrix(this Transform transform)
 {
     return Matrix4x4.TRS(transform.localPosition, transform.localRotation, transform.localScale);
 }

However I don't quite understand why you want to do that. Unity already does all that for you and most likely has all the matrices cached internally since they are required by Unity anyways.


You may also want to have a look at my Matrix crash course. Also when you want to understand matrices better, I highly recommend the 3b1b series on linear algebra

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 torano · Aug 14, 2019 at 08:39 AM 0
Share

Thanks for your replying. As I stated at first, the purpose is to learn. I didn't study matrices in my school(in Japan), but developing games with Unity, I sometimes saw matrices are used for transformation calculation. Unity usually takes care of everything about transformation stuff, but I felt uncomfortable because I didn't know what was going on inside the calculation, especially of transfomation to local/world, so I tried to do it manually though I won't have to use the manual transformation in a real development in Unity. I am happy with that you also provide learning materials, I will definitely check it out. Really appreciated.

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

208 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 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 avatar image avatar image

Related Questions

Trying to make an object "Flee" from the player but it won't work? 3 Answers

Line render from an object to a click 0 Answers

My gameobject only moves to the right once and then stops, why? 3 Answers

How to have an enemy spawn a certain distance behind the player 1 Answer

Math behind Transform.TransformPoint? 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