I want to translate the object along X and Y direction.
For that I construct a translation matrix and Multiply It to World Matrix.
D3DXMatrixTranslation(&matTrans, xDist, yDist, 0);
D3DXMatrixMultiply (&matWorld, &matWorld, &matTrans);
This works fine.
Next,I want to rotate by object around world origin along Z direction.
For that I construct a Rotation matrix and Multiply It to World Matrix.
D3DXMatrixMultiply (&matWorld, &matWorld, &matRotZ);
This rotates the object and its axes also.
Next I want to translate again.
This causes the translation to happen along the direction of rotated X,Y axes caused due to the previous rotation.
But I want the object to always translate along the World's X and Y direction not to translate along the object's axes.
What should I do for this?
Can Any one help?
Hey there !
This sounds like you keep just a single matrix for each of your objects.
Using your approach, you would want to change the place where you add your matrix multiplication.
I.e. usually, you have this:
worldposition = Matrix * objectposition;
or, in detail:
worldposition = Translation * Rotation * objectposition
with: objectposition being a point local to the reference frame of your object, and worldposition being the coordinates of same point in the global coordinate system. (Sorry, I'm not one of the DX users, so this has been created based on your textual description.)
Matrix multiplication is associative, but not commutative, which means you may put in arbitrary brackets, but may not change the order of operations.
Let's now rewrite the equation above with a few brackets:
worldposition = (Translation * (Rotation * objectposition))
As you can see, the point first is sent through the rotation matrix you created, it then is in global space, and translated to the place where it should be.
So, if you want to add another translation local to the object's coordinates, where would you place it ?
Indeed, right in front of the existing translation (which is in global space). Thus, in the above case, you'd need to multiply in your translation from the left.
worldposition = (new_translation * Matrix) * objectposition;
Another approach would be to transform the global axes with the inverse of your existing matrix, and add appropriate translation matrices for the transformed global axes to the right of your matrix stack.
Finally, note that in most cases it is better to seperate between object position and object rotation (possibly use a quaternion here instead of 3 euler angles or matrices, because both of these have severe stability issues), and rebuild an appropriate matrix when required.
Btw: If your DX matrix multiplication calls for C = A * B are matrixmult(C, A, B), which I think were the logical order, then there is something fishy in your code (since you'd have the rotation last in queue then, it would be around the objects local axes, and not around the global one as you wrote).... just guessing here though...
Hope this helps,
Basically, I wanted to develop an application which on pressing particular keys should rotate around the world origin and translate whenever the corresponding keys are pressed.
What order of transformation I should apply?
If I first Rotate and then translate, the consecutive rotation happens around the objects origin and not the world origin and the translation happens fine.
If I first translate and then rotate,the consecutive translation happens along the rotated axes and the rotation happens fine around the world origin.
You'll need to apply a translation, rotation, and then another translation (and then another rotation, if you want to rotate around the world origin again, and so on...).
Alternatively, always rotate and then translate, but when you do a rotation, apply the rotation to the current translation vector as well.
Thank you for ur reply.
I use the following code:
First construct a vector with components which I want to translate
vector1.x = xDist;
vector1.y = yDist;
vector1.z = 0;
Then Rotation matrix is constructed.
Then I transform the translation vector by the Rotation matrix.
Then construct translation matrix from the components of transformed vector:
D3DXMatrixTranslation(&matTrans, vector2.x, vector2.y, vector2.z);
Then apply translation and then rotate as
matWorld = matTrans * matRotZ ;
But then also the translation does not happen in the World X,Y.
did you try mul(world,translation) and mul(translation, world)
one puts the translation at the start of the order, and one puts it at the end.
or am i wrong?