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.
views:
804answers:
4In 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.
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.
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.