Converting to and from local and world 3D coordinate spaces?
        Posted  
        
            by 
                James Bedford
            
        on Game Development
        
        See other posts from Game Development
        
            or by James Bedford
        
        
        
        Published on 2011-03-06T11:28:32Z
        Indexed on 
            2011/03/06
            16:19 UTC
        
        
        Read the original article
        Hit count: 522
        
Hey guys,
I've been following a guide I found here (http://knol.google.com/k/matrices-for-3d-applications-view-transformation) on constructing a matrix that will allow me to 3D coordinates to an object's local coordinate space, and back again. I've tried to implement these two matrices using my object's look, side, up and location vectors and it seems to be working for the first three coordinates. I'm a little confused as to what I should expect for the w coordinate.
Here are couple of examples from the print outs I've made of the matricies that are constructed. I'm passing a test vector of [9, 8, 14, 1] each time to see if I can convert both ways:
Basic example:
localize matrix:
Matrix: 0.000000    -0.000000   1.000000    0.000000
        0.000000    1.000000    0.000000    0.000000
        1.000000    0.000000    0.000000    0.000000
        5.237297    -45.530716  11.021271   1.000000
globalize matrix:
Matrix: 0.000000    0.000000    1.000000    0.000000
        -0.000000   1.000000    0.000000    0.000000
        1.000000    0.000000    0.000000    0.000000
        -11.021271  -45.530716  -5.237297   1.000000
test:
Vector4f(9.000000, 8.000000, 14.000000, 1.000000)
localTest:
Vector4f(14.000000, 8.000000, 9.000000, -161.812256)
worldTest:
Vector4f(9.000000, 8.000000, 14.000000, -727.491455)
More complicated example:
localize matrix:
Matrix: 0.052504    -0.000689   -0.998258   0.000000
        0.052431    0.998260    0.002068    0.000000
        0.997241    -0.052486   0.052486    0.000000
        58.806095   2.979346    -39.396252  1.000000
globalize matrix:
Matrix: 0.052504    0.052431    0.997241    0.000000
        -0.000689   0.998260    -0.052486   0.000000
        -0.998258   0.002068    0.052486    0.000000
        -42.413120  5.975957    -56.419727  1.000000
test:
Vector4f(9.000000, 8.000000, 14.000000, 1.000000)
localTest:
Vector4f(-13.508600, 8.486917, 9.290090, 2.542114)
worldTest:
Vector4f(9.000190, 7.993863, 13.990230, 102.057129)
As you can see in the more complicated example, the coordinates after converting both ways loose some precision, but this isn't a problem. I'm just wondering how I should deal with the last (w) coordinate? Should I just set it to 1 after performing the matrix multiplication, or does it look like I've done something wrong?
Thanks in advance for your help!
© Game Development or respective owner