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 CalipsDZ · Mar 05, 2019 at 02:36 PM · transformtransformation

The rotation and translation between 2 similar cubes in a scene

I want to know if it's possible to compute the transformation T between two similar objects (for instance : 2cubes) knowing their two transform components, so that when you multiply T * cube1.transform = cube2.transform ?

T: is the matrix representing the difference in position and rotation between the 2 objects (their coordinate systems)

Comment
Add comment · Show 2
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 WarmedxMints · Mar 05, 2019 at 02:38 PM 0
Share

Do you mean you want to work out the difference in positions and rotations between two objects or actually move one object to occupy the same space?

avatar image CalipsDZ WarmedxMints · Mar 06, 2019 at 06:25 AM 0
Share

The first one, the difference in position and rotation

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · Mar 05, 2019 at 04:01 PM

AFAIK, you can't have this kind of operation against the Transform component unless you create your own extension method.


Even if you could, I get very strange results. Maybe I am doing something wrong... But here is how you can compute the position and rotation difference between two transforms:

 Quaternion rotationDifference;
 Vector3 positionDifference;
 
 private void ComputeDifference( Transform other )
 {
     positionDifference = other.position - transform.position;
     rotationDifference = other.rotation * Quaternion.Inverse( transform.rotation );
 }
 
 public void ApplyDifference()
 {
     transform.position += positionDifference;
     transform.rotation = rotationDifference * transform.rotation;
 }


Thanks to @ETdoFresh, and his/her answer here, with additional functions, you can have the same result with TRS matrix

 Matrix4x4 trs;

 public void ComputeDifference( Transform other )
 {
     trs = Matrix4x4.TRS( other.position - transform.position, other.rotation * Quaternion.Inverse( transform.rotation ), Vector3.zero );
 }

 public void ApplyDifference()
 {
     transform.position += (Vector3) trs.GetColumn( 3 );
     transform.rotation = ExtractRotation( trs ) * transform.rotation;
 }

 public Vector3 GetPosition( Matrix4x4 m )
 {
     return new Vector3( m[0, 3], m[1, 3], m[2, 3] );
 }

 public Vector3 GetScale( Matrix4x4 m )
 {
     return new Vector3
         ( m.GetColumn( 0 ).magnitude, m.GetColumn( 1 ).magnitude, m.GetColumn( 2 ).magnitude );
 }

 public Quaternion ExtractRotation( Matrix4x4 m )
 {
     Vector3 s = GetScale(m);

     // Normalize Scale from Matrix4x4
     float m00 = m[0, 0] / s.x;
     float m01 = m[0, 1] / s.y;
     float m02 = m[0, 2] / s.z;
     float m10 = m[1, 0] / s.x;
     float m11 = m[1, 1] / s.y;
     float m12 = m[1, 2] / s.z;
     float m20 = m[2, 0] / s.x;
     float m21 = m[2, 1] / s.y;
     float m22 = m[2, 2] / s.z;

     Quaternion q = new Quaternion();
     q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m00 + m11 + m22 ) ) / 2;
     q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m00 - m11 - m22 ) ) / 2;
     q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m00 + m11 - m22 ) ) / 2;
     q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m00 - m11 + m22 ) ) / 2;
     q.x *= Mathf.Sign( q.x * ( m21 - m12 ) );
     q.y *= Mathf.Sign( q.y * ( m02 - m20 ) );
     q.z *= Mathf.Sign( q.z * ( m10 - m01 ) );

     // q.Normalize()
     float qMagnitude = Mathf.Sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
     q.w /= qMagnitude;
     q.x /= qMagnitude;
     q.y /= qMagnitude;
     q.z /= qMagnitude;

     return q;
 }
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
0

Answer by Bunny83 · Mar 05, 2019 at 04:42 PM

It's not quite clear what you want to calculate here. A transform represents a 3d space. So you can define points in local space and they can be transferred into worldspace.


If you want the matrix which transforms from one space into the other you just have to combine the localToWorldMatrix of the source transform with the worldToLocalMatrix of the target transform. The resulting matrix allows you to directly transform a local space point from the source transform space into the target transform space.

 Matrix4x4 m = target.worldToLocalMatrix *  source.localToWorldMatrix;


Keep in mind if any of the two transforms changes you have to recalculate this matrix.

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

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

132 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

Related Questions

Transform Screen Point to Transform extension 1 Answer

Use methods of Transform without linked gameObject 1 Answer

moving sphere along the curved path 1 Answer

How to set the position of a guitext using transform? 1 Answer

Error: not a member of 'UnityEngine.GameObject[]'. 2 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