0
\$\begingroup\$

in unity when you parent an object or transform with another object, all transform(position, rotation, scale) are calculated depending on that parent object. sometimes we need to know about the local data of that transform and sometimes we need to know that data based on the world axis.

I need to know how that is implemented in math and vector functions.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Are you looking for the matrix inverse, to convert from world positions to parent-local positions/etc? What specific operations do you need help implementing? Note that much of this is visible in Unity's published C# code, if you want to see how it implements a specific vector method. \$\endgroup\$
    – DMGregory
    Mar 30 at 11:52
  • \$\begingroup\$ @DMGregory really? unity has published implementation of its math and vector classes? i think there is only a basic class for working with unity entities. all I need to know is when parent object updates any of its transforms how child object updates them and how functions like transform.transformpoint and transform.transformdirection work. \$\endgroup\$
    – virtouso
    Mar 30 at 12:26
  • \$\begingroup\$ When setting an object as a child, what you essentially do behind the scenes is that you apply the local transformation of the child then apply the local transformation of the parent which yields the resulting world transform. Is that what you were looking for? \$\endgroup\$
    – PentaKon
    Mar 30 at 12:43
  • 1
    \$\begingroup\$ You may be interested in past Q&A like matrices and nesting hierarchy with game objects / Math behind drawing one game object relative to another / Dealing with more complex entities in an ECS architecture (really about parenting) \$\endgroup\$
    – DMGregory
    Mar 30 at 13:48
2
\$\begingroup\$

Parenting an object essentially means that the object's transform now is relative to the parent's instead of the world's. If the parent object is a body for example and the child object is the arm, then the body's position in the world is defined by its bodyTransform and is relative to the world coordinates but the armTransform is relative to the body's space, which I believe is done via a change of basis on the arm object where the new basis is the body's local basis. This means that you can individually change the arm's position by changing its transform relative to the body (which is what you care about) but if the whole body moves, the arm moves with it since its armToWorld transform already contains the bodyTransform inside.

\$\endgroup\$
1
\$\begingroup\$

You don't really need to know how that is implemented in math and vector functions. Not having to bother with stuff like that is one of the reasons why you are using a game engine in the first place :) But if you want to know because it interests you:

Internally, Unity implements the whole data of the Transform component with a transformation matrix. A transformation matrix is a collection of 4x4 floating-point values. Encoded in these values is the translation, rotation and scale (and technically also skew, but that's rarely used in 3d graphics).

There is a way to multiply a point (represented as a Vector) with a projection matrix which results in a new point with the rotation, translation and scale applied to it. You can also multiply two projection matrices with each other to get a single new 4x4 matrix which applies the transformations of both matrices one after another. This is how the matrices of transforms in a parent hierarchy are calculated. Unity actually exposes that matrix of a transform and its parent transforms through the property localToWorldMatrix.

OK, but how exactly does that multiplication of a vector with a matrix or a matrix with a matrix work? Lots of people have already written lots of explanations for that which are far better than I could write.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.