World to Local Space

Started by
2 comments, last by RobTheBloke 12 years, 8 months ago
So I have 2 Objects. ObjectA and ObjectB. I would like to convert ObjectB's position/orientation relative to ObjectA.
I have seen others use the transpose of ObjectA's world matrix to convert other objects into its local space. How does this work?

xdpixel.com - Practical Computer Graphics

Advertisement
[font="arial, verdana, tahoma, sans-serif"]

So I have 2 Objects. ObjectA and ObjectB. I would like to convert ObjectB's position/orientation relative to ObjectA.
I have seen others use the transpose of ObjectA's world matrix to convert other objects into its local space. How does this work?

Not at all (at least not in general). You need the inverse matrix instead.

A world matrix defines how to transform a vector given in local space to yield in the same vector but w.r.t. the global space. When using column vectors, this looks like[/font]
p[sub]W[/sub] := W * p
The inverse matrix hence defines how a vector given in global space is to be transformed to yield in the same vector but w.r.t. the local.space, because of
W[sup]-1[/sup] * p[sub]W[/sub] = W[sup]-1[/sup] * ( W * p ) = p

If W[sub]A[/sub] is the world matrix of object A, and similarly W[sub]B[/sub] is the world matrix of object B, then
W[sub]B[/sub] * [ 0 0 0 1 ][sup]t[/sup]
gives you the local origin of B w.r.t. the global space. Further then
W[sub]A[/sub][sup]-1[/sup] * W[sub]B[/sub] * [ 0 0 0 1 ][sup]t[/sup]
gives you the local origin of B w.r.t. the local space of A. In general,
M := W[sub][/sub][sup]-1[/sup] * W[sub]B[/sub]
transforms any vector given in local space of B into the local space of A.

Only in the special case that W is a pure rotation (i.e. ortho-normal) matrix you have the correspondence
W[sup]-1[/sup] == W[sup]t[/sup]
Thanks haegarr! So if I want to move Object B into the local space of Object A, then I should do the following...

[color="#1c2837"]vecObjectBLocalSpacePosition = ObjectB.GetPosition() * ObjectA.GetWorldMatrixInverse();
[color="#1c2837"]

[color="#1c2837"]That would give me Object B's local space position realtive to Object A if Object A were centered at the origin correct? Thanks!

xdpixel.com - Practical Computer Graphics


[color="#1c2837"]That would give me Object B's local space position realtive to Object A if Object A were centered at the origin correct? Thanks!


That will depend how you math library is set up. Normally I'd expect to see :

[color="#1c2837"][source]vecObjectBLocalSpacePosition = ObjectA.GetWorldMatrixInverse() * ObjectB.GetPosition();[/source]

but that is down to your library i guess. There are quite few libraries where vec * matrix is not the same as matrix * vec. Definitely one you'd need to check with your lib ;)

This topic is closed to new replies.

Advertisement