- Home /
InverseTransformPoint via Matrices?
I wish to convert a point from a world space to a local space without having to create and delete a GameObject each time to do it. I therefore wanted to do via matrices. Instead of doing:
GameObject joint = new GameObject("TempJoint");
joint.transform.localPosition = jointData1.JointPosition;
joint.transform.localRotation = jointData1.JointRotation;
joint.transform.localScale = jointData1.JointScale;
joint.InverseTransformPoint( pointToTransform );
I hoped that I would be able to do:
Matrix4x4 matrix4x4 = Matrix4x4.TRS( jointData1.JointPosition, jointData1.JointRotation, jointData1.JointScale );
matrix4x4.inverse.MultiplyPoint( pointToTransform );
However - they come out with completely different kinds of values. Is anyone able to give me a hint as to what I might be doing wrong?
Thanks!
Answer by NathanJSmith · Oct 20, 2018 at 10:50 AM
Quick code for future reference:
Matrix4x4 matrixTrans = Matrix4x4.identity;
matrixTrans.SetTRS(m_GameObject.transform.position, m_GameObject.transform.rotation, Vector3.one);
//Matrix4x4 version of Transform.InverseTransformDirection
Vector3 localDirection = matrixTrans.inverse.MultiplyVector(worldDirection);
//Matrix4x4 version of Transform.InverseTransformPoint
Vector3 localPosition = matrixTrans.inverse.MultiplyPoint3x4(worldPositon);
Answer by Bunny83 · Jun 10, 2015 at 12:19 PM
I've seen this question yesterday but had no time to answer ^^.
The way you presented your problem i can't see anthing wrong here. However you might mix up the different spaces you're working in. You said you want to convert "a world" space (position) into a local space (position). There's usually only one world space. Every other space are different local spaces.
Your two examples are exactly the same as long as your "TempJoint" has no parent and therefore is located in world space. As soon as a parent is involved it get's tricky as a sub localSpace can't necessarily be correctly defined with a simple translation / rotation / scale depending on the parents values. The scale is what makes problems in the first place. If everything is scaled uniformly (or not at all) the end space can be represented with a world position + rotation + scale.
That's also the reason why Unity doesn't have a "scale" (world-scale) property but only a lossyScale property.
Anyways, since your code doesn't seem to involve any kind of parent information (even though the name "joint" suggests some kind of hierarchy) your two approaches work exactly the same.
If this is just abstracted pseudo code, post your actual setup. In what space are your JointPosition, JointRotation and JointScale ?
What do you actually want to calculate (in respect to your setup, which we don't know)?
ps.: Even i know it should work exactly the same i actually did setup a test, calculated the same incoming position in those two ways and placed a cube at the according position and they always match perfectly, no matter what i do to the incoming "space parameters". Even using a parent and using position, rotation and lossyScale both produce exactly the same (approximated) result.
So if you need further help, be more specific about your setup and what spaces you actually talk about.
Your answer
Follow this Question
Related Questions
How to get translation from matrix? 1 Answer
Set shader builtin matrices by script 0 Answers
Can't set GUI.matrix... 1 Answer
How to get the Quaternion Rotation from a Matrix4x4 2 Answers