0
votes

When building a world projection matrix from scale, rotate, translate matrices, then the translation matrix must be the last in the process, right? Else you'll be scaling or rotating your translations. Do scale and rotate need to go in a specific order?

Right now I've got

std::for_each(objects.begin(), objects.end(), [&, this](D3D93DObject* ptr) {
    D3DXMATRIX WVP;
    D3DXMATRIX translation, rotationX, rotationY, rotationZ, scale;
    D3DXMatrixTranslation(&translation, ptr->position.x, ptr->position.y, ptr->position.z);
    D3DXMatrixRotationX(&rotationX, ptr->rotation.x);
    D3DXMatrixRotationY(&rotationY, ptr->rotation.y);
    D3DXMatrixRotationZ(&rotationZ, ptr->rotation.z);
    D3DXMatrixScaling(&translation, ptr->scale.x, ptr->scale.y, ptr->scale.z);
    WVP = rotationX * rotationY * rotationZ * scale * translation * ViewProjectionMatrix;
});
1

1 Answers

2
votes

You are correct in thinking that the order matters. Matrix multiplication is not commutative.

As for the specific question. If you are doing a non-uniform scaling, then it certainly matters which order you apply the transformations. You probably want to do the scaling first, because then the scaled coordinate is still it's original, local coordinate system. When you want to make the tree taller, but not wider, you would scale the Y coordinate. If you want a tall tree on the side of a hill, you'd then rotate it as needed.

On the other hand, scaling might be a result of some other interaction. The character is stuck under a moving ceiling and it's squishing him. In that case, the character needs to be rotated into position, and then scaled downward.

Ultimately, what this means is that you need to apply the transformations in the order that they occur. scale the object into the desired shape, then make the needed transformations to put them into the world as needed. Add any extra steps in the middle according to your exact need.