Reputation: 9357
Assume I have 2 matrices :
MatrixA = translation1 * scale1 * rotation1
MatrixB = translation2 * rotation2 * translation3
Is MatrixA * MatrixB
equals to translation1 * scale1 * rotation1 * translation2 * rotation2 * translation3
? Or something else near ?
In fact I just want to know if I can avoid multiplying matrices together and save some CPU by using only 1 matrix and then chain up mathematical operations.
Because I plan to make a little custom 2D engine which would be built upon LibGDX, including a graphic components API similar to what you can find in the Android SDK. Anyway there will be some components, those that are in containers, that will need to move when their parents move. So I need these imbricated arithmetic. I know it's possible using matrices but as it is supposed to run on mobile devices I don't want it to be too slow.
Upvotes: 1
Views: 322
Reputation: 1598
You are indeed correct. You can avoid having more than one matrix by multiplying a chain of them together. However, do remember that Matrix Multiplication is non-commutative which means that changing the order of the operations will result in two different matrices. For example:
matrixTranslate * matrixRotate
will result in an object rotating around a point, while:
matrixRotate * matrixTranslate
will result in an object rotating around its own central axis. No order of multiplying matrices is wrong, however you have to be aware of the different results.
Having said this, in your question:
MatrixA = translation1 * scale1 * rotation1
MatrixB = translation2 * rotation2 * translation3
MatrixC = MatrixA * MatrixB
This is 5 matrix multiplication operations. If you were to chain them all together:
MatrixC = translation1 * scale1 * rotation1 * translation2 * rotation2 * translation3
You would also be doing 5 matrix multiplication operations. As you can see, there is no difference in actual operations. You are technically allocating one extra matrix and depending on your implementation and frequency it may make a difference, but in terms of mathematical operations, it is the same.
Upvotes: 2