Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

problem with affine transformation

This topic is 3790 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

hi, I have a 4x4 matrix which looks a bit like this:
double matrix[][] = {
{ 1.0, 0.0, 0.0, 0.0 } // X axis vector
{ 0.0, 1.0, 0.0, 0.0 } // Y axis vector
{ 0.0, 0.0, 1.0, 0.0 } // Z axis vector
{ 0.0, 0.0, 0.0, 1.0 } // translation vector
};

the problem appears when I try to transform a point through this. First I convert the point into a vector:
double converted[][] = {
{ point.x, point.y, point.z, 1.0 }
};

then multiply it by the matrix using this function:
   public double[][] multiply(double[][] matrix1, double[][] matrix2) {

         double result[][] = new double[matrix1.length][matrix2[0].length];
         for(int i = 0; i < matrix1.length; i++) {
           for(int j = 0; j < matrix2[0].length; j++) {
              for(int k = 0; k < matrix2.length; k++) {
                 result[j] += (matrix1[k] * matrix2[k][j]);
              }
           }
         }
         return result;

   }

here's an example - say the translation vector was (300, 300, 300) and the matrix looked like this:
{   1.0,   0.0,   0.0, 0.0 }
{   0.0,   1.0,   0.0, 0.0 }
{   0.0,   0.0,   1.0, 0.0 }
{ 300.0, 300.0, 300.0, 1.0 }

then I apply a z rotation of 180 degrees so it now looks like this:
{  -1.0,   0.0,   0.0, 0.0 }
{   0.0,  -1.0,   0.0, 0.0 }
{   0.0,   0.0,   1.0, 0.0 }
{ 300.0, 300.0, 300.0, 1.0 }

now I want to translate the point (100, 100, 100), before I even multiply it by the matrix I know the translated point will be (200, 200, -200) but when I multiply it by the matrix it gives me (200, 200, 400) which makes sense(in matrix multiplication) but is not the correct answer???

Share this post


Link to post
Share on other sites
Advertisement
Mighty MATLAB says the following.

>> v = [100, 100, 100, 1]
v =
100 100 100 1
>> M=[-1, 0, 0, 0; 0, -1, 0, 0; 0, 0, 1, 0; 300, 300, 300, 1]
M =
-1 0 0 0
0 -1 0 0
0 0 1 0
300 300 300 1
>> v*M
ans =
200 200 400 1

You problem is that you probably expect transformations to happen in one order, but you apply them in another. Translating first and then rotating does not give you the same result as when rotating first and then translating.

The result you get, given the matrices and vectors you mentioned, is correct.

Share this post


Link to post
Share on other sites
I don't understand what you mean... I translated the matrix so that it looked like:

[ 1.0, 0.0, 0.0, 0.0 ]
[ 0.0, 1.0, 0.0, 0.0 ]
[ 0.0, 0.0, 1.0, 0.0 ]
[ 300.0, 300.0, 300.0, 1.0 ]

then i rotated about the z axis, would it not just be the same result whatever way you done it?

the way im translating is just by setting the 3 elements of the matrix:

matrix[3][0] = x;
matrix[3][1] = y;
matrix[3][2] = z;

but i take it i have to multiply the matrix by another?

Share this post


Link to post
Share on other sites
Translation is not just about setting three elements of a matrix. A transformation is applied by multiplying by the corresponding transformation matrix. So if you want to translate a rotation matrix, you multiply the rotation matrix by the translation matrix.

And since matrix multiplication is not commutative, order of multiplication matters. That is, apply transformations in different order and you get different results. Some MATLAB output again.

>> T=[1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 300, 300, 300, 1]
T =
1 0 0 0
0 1 0 0
0 0 1 0
300 300 300 1
>> R=[-1, 0, 0, 0; 0, -1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1]
R =
-1 0 0 0
0 -1 0 0
0 0 1 0
0 0 0 1

There we have your two transformation matrices; a translation matrix and a rotation matrix. Now apply them in two diffenent orders to see the final matrix.

>> T*R
ans =
-1 0 0 0
0 -1 0 0
0 0 1 0
-300 -300 300 1
>> R*T
ans =
-1 0 0 0
0 -1 0 0
0 0 1 0
300 300 300 1
>>

See how the matrices depends on the order of transformation?

Share this post


Link to post
Share on other sites
Show us what you rotation and translation matrices look like, what you get when multiplying them, and what you get when multiplying the result by the vector. All I know at the moment is what you get and that it's wrong. Not sure what you start with really.

Share this post


Link to post
Share on other sites
here's the z axis rotation matrix:

double z_rotation_matrix[][] = {
{ cos(z_rot), -sin(z_rot), 0.0, 0.0 },
{ sin(z_rot), cos(z_rot), 0.0, 0.0 },
{ 0.0, 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 0.0, 1.0 }
};


here's the translation matrix:

double translation[][] = {
{ 1.0, 0.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0, 0.0 },
{ 0.0, 0.0, 1.0, 0.0 },
{ x, y, z, 1.0 }
};



I have one initial 4x4 matrix which i multiply by the rotation matrix and then the translation matrix. I thought you only had to have one matrix for all the translations/rotations etc.
I'm coding a camera class where you can manipulate the location(translation vector) and the rotation on all axis's and I thought I could just use one 4x4 matrix for each camera.

Share this post


Link to post
Share on other sites
And in what order do you multiply them? z_rotation_matrix*translation or translation*z_rotation_matrix?

First one gives you [200, 200, 400], second one gives you [-400, -400, 400].

Share this post


Link to post
Share on other sites
I don't directly multiply them::

camera_matrix = translation_matrix * camera_matrix
camera_matrix = rotation_matrix * camera_matrix;

i've tried it this way and this way:

camera_matrix = rotation_matrix * camera_matrix
camera_matrix = translation_matrix * camera_matrix;

Share this post


Link to post
Share on other sites
You are multiplying them. Maybe not directly in your code, but indirectly since you cumulate transformations in the camera matrix.

I kind of have a feeling you're not really understanding with how transformations works. Seems like you're mostly guessing how to do to get the result you're expecting without knowing what's right and wrong. Can be dangerous in this case as two errors can make a right, and it won't show in your tests.

I can't recommend any tutorials or something on this subject as I don't know of any suitable, but shouldn't be hard to find them. Also recommend you find an already made vector and matrix library.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!