[SOLVED] Set world position into a child matrix

Consider the following setup: object A contains object B, which contains object C.
All objects have local position, scale and rotation. They calculate a local matrix + world matrix, where ‘world matrix’ is the combination of their own local matrix and their parent matrix.

For example, object C’s world matrix is its C.LocalMatrix * B.WorldMatrix.
B’s World Matrix would be B.LocalMatrix * X.WorldMatrix and so forth (until we get to a parentless object that just return local matrix as world matrix).

Or in other words, C’s final world matrix is:
A.LocalMatrix * B.LocalMatrix * C.LocalMatrix.

Now if I want to set C’s position in final world space.
For example, I want to be able to do this:

C.Position = new Vector(10, 10, 10);

and C’s world matrix position would be exactly 10,10,10, regardless of A and B’s transformations, or even own C’s rotation / scale.

If you’re familiar with Unity - basically I’m asking how they set objects world position :slight_smile:

Thanks!

Edit: just to be clear, I’m not only interested in setting the final world matrix position, that would be easy :slight_smile: I want to actually calculate and store the local position value after setting the world position, since I want to be able to set both world position and relative local position…

You have a combined world matrix

Matrix worldMatrix = MatrixA * MatrixB * MatrixC * ...

You can use that matrix to transform some local position to world space

Vector3 worldPosition = worldMatrix.Transform(localPosition);

What you want is the exact opposite. You want to go from world space to local space. You can do that by using the inverse of the world matrix.

Vector3 localPosition = Matrix.Invert(worldMatrix).Transform(worldPosition);
1 Like

Almost, you need to take the world matrix of the parent without self position :slight_smile:
Thanks!