PDA

View Full Version : Matrix Multiplication Question


Raptisoft
11-28-2004, 03:37 PM
Hi all,

I have a question for some of you real mathheads out there. I have a matrix class that encapsulates matrix functions (essentially, it just wraps D3DX functions).

I'm trying to draw text on a slant. Can one of you tell me what the difference is between these two code chunks? The premise is to rotate an object around a center after offseting the object 100 pixels along X. Then draw it with the center at 100,100.

Matrix myMatrix;
myMatrix.Translate(100,0);
myMatrix.Rotate2D(45);
myMatrix.Translate(100,100);

Okay, that one works. Now, I thought this would do the exact same thing:

Matrix myMatrix;
myMatrix.Translate(100,0);

Matrix myOtherMatrix;
myOtherMatrix.Rotate2D(45);
myOtherMatrix.Translate(100,100);

myMatrix*=myOtherMatrix;

That second one doesn't work properly. The object is rotated as though it was at 0,0-- around its own arbitrary center. But it draws in the correct location, meaning that the translate in myOtherMatrix definitely DID happen, but seems to get applied before the rotate instead of after! Is this some artifact of matrix multiplication that I don't know about?

Thanks a skajillion!

--John

mahlzeit
11-29-2004, 09:48 AM
The order in which matrixes are multiplied matters. If A and B are two matrixes, then A * B is different from B * A. In your first case, you do (A * B) * C. In the second case, you do A * (B * C). See also here (http://en.wikipedia.org/wiki/Matrix_multiplication).

ggambett
11-29-2004, 09:52 AM
I'm sure it was a typo, but (A*B)*C is equivalent to A*(B*C). Matrix multiplication is not commutative (A*B != B*A, generally) but it is associative ( (A*B)*C == A*(B*C))

What you can't do, and I think mahlzeit tried to say, is (A*B)*C != C*(A*B).

Raptisoft
11-29-2004, 10:00 AM
Oh, I got it last night.

Essentially, I can't do what I want to do. It's an order of operations thing. I was translating, AND multiplying, so it was like trying to say:

(A+B)*C

is the same as

A+B*C

ggambett
11-29-2004, 10:16 AM
Translations can be expressed as matrix multiplications, not additions, if you use homogeneous coordinates (4x4 matrices in 3D), which OpenGL and lesser APIs do. You probably can do what you want to do, it depends on the semantics of translate(), rotate() and multiply() (do they pre-multiply, post-multiply, add, or what?)

mahlzeit
11-29-2004, 11:16 AM
Whoops, my bad. Listen to Gabriel instead! :)

Raptisoft
11-29-2004, 01:04 PM
Well, essentially, all my functions make a new matrix with the info you requested, then multiply that new matrix by the existing matrix.

It all just uses D3DXMatrix functions-- like D3DXMatrixMultiply, etc. I try not to muddle the creativity with unnecessary math. ;)

So, I would say I'm doing a matrix multiply every time.