# Matrix Rotations

I have a world being drawn and I can move around correctly, however I can’t get the rotation to work right. It always rotates around the (0,0,0) point. I have a function call to do rotations and translations and switching the order doesn’t make any difference.

How can I perform a translation then rotation with matrices as if I was doing a glTranslate(), and then a glRotate()?

Apply the traslation matrix first and then apply the rotation matrix. Supposing your translation matrix is T and the rotation matrix is R. If you concatenate the matrix like this

``````M = T*R;
``````

The concatenated matrix M will do rotation first and then translation. If however, you concatenate them this way,

``````M = R*T;
``````

This will translate first and then rotate.

That is what I thought, but when I change the order in the function call:

``````Multiply(mModelView, mRotate, mTranslate);
``````

to

``````Multiply(mModelView, mTranslate, mRotate);
``````

It makes absolutely no difference in the end result. I suppose this is a problem with applying the multiplication directly to the mModelView matrix instead of a temporary matrix. Thank you for the clarification on how it should work.

Even if u take a temp matrix, matrix multiplication is non commutative so u should get a different result by changing the order (unless one of your matrix is identity). A simple example: Rotate on Z by 45 and then translate by 0,0,10. Translate 0,0,10 and then rotate on Z by 45 bring u to two different locations in 3d space.

I think u might be modifying the matrices elsewhere that may be causing this behaviour.

I would like to help with this. Can you provide the actual code you are using inside your Multiply function, and also the snippet of code that surrounds the function call? I would like to see how or if you are using glPushMatrix and glPopMatrix.

I have partially solved this problem, I was overwriting the mModelView matrix instead of “appending” to it. I havn’t had time to code the solution yet as I have a test I have been studying for.

I’m using some of the math from the superbible, here is the multiply function from that:

``````
void Matrix::Multiply(Matrix44f &product, const Matrix44f a, const Matrix44f b )
{
#define A(row,col)  a[(col<<2)+row]
#define B(row,col)  b[(col<<2)+row]
#define P(row,col)  product[(col<<2)+row]

for (int i = 0; i < 4; i++) {
float ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
}
}

``````

I currently do not have a working pop/push, but the idea is to use a memcopy call such as:

``````
void Matrix::Push() {
memcpy(saveMatrix, mModelView, sizeof(Matrix44f));
}

void Matrix::Pop() {
memcpy(mModelView, saveMatrix, sizeof(Matrix44f));
}

``````

I haven’t looked up if this will have any speed consequences yet using this method.