views:

804

answers:

4

if I do positionVector*worldMatrix the position is transformed into world space. But what happens if I do it the other way around (worldMatrix*positionVector) in terms of 3d space?
I noticed the result is different to the first one. I already googled about matrix, math they explain a lot but not this one, at least I couldn't find it.

+8  A: 

In matrix*vector, your vector will be interpreted as a column vector. In vector*matrix, it will be interpreted as a row vector. 2x2 examples:

/ a b \   / e \   / ae+bf \
|     | * |   | = |       |
\ c d /   \ f /   \ ce+df /

          / a b \
( e f ) * |     | = ( ea+fc eb+fd )
          \ c d /

As you can see, the result is different.

Incidentally, doing the one is the same as doing the other after transposing the matrix.

In terms of 3D space, if you consider one of the two options to be a linear transformation, I don't know if there is any sensible interpretation for the other one. This Wikipedia section says things about it, but it is beyond my understanding of linear algebra.

Thomas
+2  A: 

(matrix * vector) is equivalent to (vector * transpose(matrix))

Simon C.
+1  A: 

Matrix math rules:

Given matrices A and B, with sizes MxN and OxP,

  • The matrix product A * B is only defined if N=O.
  • The result will be a matrix with size MxP.

Another important rule is that matrix multiplication is not commutative. A * B != B * A

Typically in computer graphics, the position vector is a 4x1 matrix, and the world view matrix is square, 4x4. Thus you should expect that pre-multiplying the world view matrix with the position vector would be undefined. The proper way to apply the world view matrix to the position vector is in the other order, pre-multiplying the position vector with the world view matrix. (I'm speaking mathematically, here)

For more fun with matrix math, check out this tutorial.

Scottie T
Thanks for the link but in HLSL (a shader language) there is a function mul(mat, vec) as well as mul(vec,mat). In the one vec is interpreted as column vector and in the other it is interpreted as row vector.
codymanix
Yes, any good graphics library/language should have a function to do this for you, but knowing what goes inside that function is useful.
Scottie T
+8  A: 

As others have indicated - swapping the order of the multiplication is equivalent to multiplying by the transpose. As it happens, rotation matrices are a special type of matrices known as orthogonal matrices this gets you a number of neat properties.

The most interesting is probably that the transpose of the matrix is its inverse. For your world transform multiplying by the inverse is equivalent to taking a position in world space and pulling it into the local coordinates of the object that transform is associated with.

As an example, consider a box oriented arbitrarily in the world - multiplying by the inverse world transform could (entirely application dependant of course :)) put you in a space where it is axis aligned, and if you were interested in looking for collisions with other objects doing the calculations in the box's local space would make this easier.

Andrew Khosravian
You are saying that inverse(mat) is the same then transpose(mat)? I cannot find any proof of that as this contradicts my school knowledge.
codymanix
In general the transpose isn't the same as the inverse. This is a specific property of orthogonal matrices (check the link in my response for more detail) which all rotation and reflection matrices are.
Andrew Khosravian