• Advertisement
Sign in to follow this  

Invert Transform Matrix [Solved]

This topic is 2134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble inverting my object matrix with non-uniform scaling. I have scanned the web and written the multiplication out by hand, but I haven't been able to find my error. I am creating a matrix for OpenGL, and I combine my 4x4 matrix in the order Translation, Rotation, Scale. Where could I be going wrong?

I find the position X,Y,Z in the last column
I find the scaling X,Y,Z as the lengths the first three columns
I use the upper left 3x3 matrix as the rotation

~ Inv Scale ~ ~ Inv Rot ~ ~ Inv Pos ~
[1/X 0 0 0] [\.......] [1 0 0 -X]
[ 0 1/Y 0 0] [..flip..] [0 1 0 -Y]
[ 0 0 1/Z 0] [diagonal] [0 0 1 -Z]
[ 0 0 0 1] [.......\] [0 0 0 1]


After combining the above matrices in that order, I come up with the following code with matrix A as the result:

(GL Matrix Indices)
[0 4 8 12]
[1 5 9 13]
[2 6 10 14]
[3 7 11 15]

void invertMatrix (float A[], float B[]) {
float sclX = lengthVector(&B[0]);
float sclY = lengthVector(&B[4]);
float sclZ = lengthVector(&B[8]);

A[0] = B[0]/sclX; A[4] = B[1]/sclX; A[8] = B[2] /sclX;
A[1] = B[4]/sclY; A[5] = B[5]/sclY; A[9] = B[6] /sclY;
A[2] = B[8]/sclZ; A[6] = B[9]/sclZ; A[10] = B[10]/sclZ;
A[3] = 0.0; A[7] = 0.0; A[11] = 0.0;

float posX = -B[12],
posY = -B[13],
posZ = -B[14];

A[12] = A[0]*posX + A[4]*posY + A[8] *posZ;
A[13] = A[1]*posX + A[5]*posY + A[9] *posZ;
A[14] = A[2]*posX + A[6]*posY + A[10]*posZ;
A[15] = 1.0;
}


To test my inverse, I created a matrix with the following transform:
Position x=0, y=0, z=0
Rotation x=1, y=0, z=0
Scaling x=1, y=1, z=3
I multiply the inverse times the matrix, which always returns the scaling on the diagonal.

-- Object Matrix -- -- Inverse Matrix -- -- Inverse * Object --
[1.0 0.0 0.0 0.0] [1.0 0.0 0.0 -0.0] [1.0 0.0 0.0 0.0]
[0.0 0.5 -2.5 0.0] [0.0 0.5 0.8 -0.0] [0.0 1.0 -0.0 0.0]
[0.0 0.8 1.6 0.0] [0.0 -0.8 0.5 0.0] [0.0 0.0 3.0 0.0]
[0.0 0.0 0.0 1.0] [0.0 0.0 0.0 1.0] [0.0 0.0 0.0 1.0]

Share this post


Link to post
Share on other sites
Advertisement
When you take the inverse of a product of matrices, you need to compute the product of the inverses in reverse order . That might explain some of what you are seeing.

However, even if you don't get back to the identity matrix, you should get a matrix with determinant 1, and yours has determinant 3, so there's probably some other mistake.

Can you be more explicit about the matrices you are multiplying? In particular, I don't know what "rot(1,0,0)" means.

Share this post


Link to post
Share on other sites
you need to compute the product of the inverses in reverse order[/quote]
I'm assuming that I did this part correctly - I performed Scaling * Rotation * Translation. I flipped the 3x3 rotation section along the diagonal, so the transformed indices (OpenGL) would be..


[0 1 2]
[4 5 6]
[8 9 10]


I don't know what "rot(1,0,0)" means.[/quote]
I modified the description of my test. They are the values that I use in my object matrix, which is combined in the order Translation, Rotation, Scale.

Also for completeness, this is how I create the matrix. Position, rotation, and scale are arrays with three floats.

mat[12] = node->position[0];
mat[13] = node->position[1];
mat[14] = node->position[2];

float A = cos(node->rotation[1]), B = sin(node->rotation[1]);
float C = cos(node->rotation[0]), D = sin(node->rotation[0]);
float E = cos(node->rotation[2]), F = sin(node->rotation[2]);

mat[0] = ( A*E + B*D*F) * node->scale[0];
mat[1] = ( C*F ) * node->scale[0];
mat[2] = (-B*E + A*D*F) * node->scale[0];

mat[4] = ( B*D*E - A*F) * node->scale[1];
mat[5] = ( C*E ) * node->scale[1];
mat[6] = ( B*F + A*D*E) * node->scale[1];

mat[8] = ( B*C ) * node->scale[2];
mat[9] = (-D ) * node->scale[2];
mat[10]= ( A*C ) * node->scale[2];

Share this post


Link to post
Share on other sites
I use the invertMatrix function from the first post. Argument A is the resulting inverted matrix, and B would be the object's matrix.

Share this post


Link to post
Share on other sites
In your original example, how did you end up with a 4.0 at index 14? If your position vector is (0,0,0) that shouldn't happen... Also, can you print out those matrices at the bottom of the first post with more precision?

Share this post


Link to post
Share on other sites
how did you end up with a 4 in position 14?[/quote]
Sorry - those are remains from an earlier test that I didn't change correctly.

Also, can you print out those matrices at the bottom of the first post with more precision?[/quote]
I'll post directly from the console to prevent typos..

Object
1.000000 0.000000 0.000000 0.000000
0.000000 0.540302 -2.524413 0.000000
0.000000 0.841471 1.620907 0.000000
0.000000 0.000000 0.000000 1.000000
Inverse
1.000000 0.000000 0.000000 -0.000000
0.000000 0.540302 0.841471 -0.000000
0.000000 -0.841471 0.540302 0.000000
0.000000 0.000000 0.000000 1.000000
Inverse * Object
1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 -0.000000 0.000000
0.000000 0.000000 3.000000 0.000000
0.000000 0.000000 0.000000 1.000000


Note: I wonder if the inverse of scale (1/scale) should be squared? Perhaps I should get another piece of paper and work through the math.

Also, here is code that compiles to demonstrate the problem. http://pastebin.com/ks07zNm2
Name the file as Matrix.c then compile with the command 'gcc -o Matrix Matrix.c -lm'

Share this post


Link to post
Share on other sites
Since the problem seems to be with the non-uniform scaling, I would write a test with no rotation first.

I think you are correct, and you need to divide by the square of the lengths (which will save you computing the square roots).

Share this post


Link to post
Share on other sites
Thank you, it appears to be working. I'm assuming that 1/scale will remove scaling on the inverse, but that alone won't remove the scaling multiplied with the original matrix, which is why I have to square it. I just hope that this is correct and won't end up with bugs later! rolleyes.gif

Here is the fixed code: http://pastebin.com/EMu7zjcF
And the compile command: gcc -o Matrix Matrix.c -lm

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement