- Home /
Why on earth can I not just get and set the Matrix4x4 of a Transform?!?
Hi
I have a C++ game development background, and am almost entirely flummoxed by the Transform class that is used to maintain the heirarchical scene graph in Unity.
Why on earth can I not just get and set the Matrix4x4 of a Transform?
The inclusion of rotation / position / scale in the class interfaces is understandable, as there's quite a lot to take in in order to get one's head around matrices.
I'm having to go around the houses for what should be the simplest of tasks.
For example: orphaning a Transform from its parent Transform and then re-positioning it with the same world space transform it had when it was attached.
This would be a single matrix multiply and matrix assignment in most of the game engines I've worked with.
My best guess as to how to do it using Transform is to copy position & rotation and then re-set them on the object after it's been orphaned - but there's no way to ensure scaling gets carried over correctly.
Am I missing something obvious about the way Transform works? e.g. am I supposed to be doing the matrix multiply and setting the At / Up / Right by hand?
cheers,
darbotron
Answer by Daniel-Brauer · Aug 10, 2010 at 08:51 PM
Unity uses Transforms for rendering and for physics interactions. To this end, it does not store transformation matrices explicitly. Translation, rotation and scale are actually stored as vectors and quaternions, and transformation matrices are generated as needed.
Allowing the user to set arbitrary transformation matrices would make it difficult for the API to infer translation, rotation and scale in a lot of cases. Without valid TRS values, the physics engine would be very unhappy. In Unity 2.6, mesh scaling is done on the CPU, which also requires a separable scale (this behaviour is changing in Unity 3).
In the specific case you mention (keeping a Transform's world-space position the same while changing its parent), Unity actually does this automatically when you change Transform.parent. If you want to work around this behaviour, you have to record the Transform's local TRS, change the parent, and then re-assign the TRS values.
I know it might seem as if Unity is holding your hand too much, but this system is a lot safer and usually lets you do what you want in less code than if you were dealing with matrices the whole time.
I agree with this. Even doing fairly advanced vector math (RB controllers etc), I rarely find a need to use matrices directly. It's mostly about letting go of preconceived ideas of how a 3D engine should expose its API. Start thinking about what you want to achieve, and stop thinking about matrices.
Gotcha.
"Unity internally uses Position, Rotation, and Scale rather than $$anonymous$$atrices" is exactly the sort of answer I wanted :)
Cheers dude :)
Ah, this is really the kind of conversation I'd love to see more frequently on Unity Answers. Brings things into balance with all the "plz write $$anonymous$$$$anonymous$$O for me"-type of questions ;-)
This would be all fine and good if unity used this kind of 'complex' transform type for all transform types. However there are a few instances when unity will return a 'simple' transform (typical 4x4 matrix) that isn't helpful to the default transform type. I.e. if you get the bind pose of a bone and try to set said bones transforms back to the bind pose you'll have to go and create the n number of matrix transformation code to extract out the position, rotation and scale from the bone bind pose 'simple' transform to then apply them back to the 'complex' transform.
Answer by Lesliehdez · Feb 16, 2018 at 06:48 PM
I need to apply this matrix: "transformation":[15,0,0,0,0,15,0,0,0,0,15,0,0,0,0,1]
and apply it to a gameobject (.obj 3D MODEL )to obtain a new transformation. How can i do that? Thank you.
Your answer
Follow this Question
Related Questions
Matrix to transform bone orientation to world orientation 0 Answers
How is localScale adjusted when re-parenting transforms? 2 Answers
How to use Matrix4x4? 2 Answers
"Baking" Object scale into mesh 1 Answer