I want to combine the transformations from two matrices into one. I have a scene that contains different shapes - rectangles, circles,.. that has it own matrix that contains all the transformations: rotation, scale, translation. When I apply transformations to the scene all elements that are inside the scene will get that transformation. This works well using this code from this repository that is used to apply transformation using finger gestures. Those image and shapes are generated with OpenGL GLES2 using this library. I am not presenting the full source code, since the problem comes from the order of the matrices as discussed below.
But I want for certain shapes to have separate matrix with transformations, that wat the shape get the transformations from their particular matrix and also from the transformations from scene. That means I need to combine the transformations from the two matrices and use it for that shape.
Lets say the scene has the matrix m1 and a image(the lion image) has the matrix m2, then I need to combine the transformations from those two matrices. I have tried using preConcat, and first apply transformations from the scene and then apply those for the shape.
var m = Matrix()
m.preConcat(m1)
m.preConcat(m2)
Then apply the transformations from the matrix m to the shape, that way it receives the combined transformations from both matrices. This works well when the transformations for the scene are changed. So when I scale, rotate, translate the whole scene will all shapes, that shape receive the transformations from both matrices successfully.
Changing the transformations for the whole scene, that is applied to all shapes (Working!)

But the problem comes when I try to change only the transformations for that particular shape, using the same code. When I try that I have problem with the transformations and the pivot point for the rotation and scaling is changed to the center of the screen, instead how it suppose to be and the pivot should match the center between the two fingers. The circle inside the triangle matches the middle of the screen, so once the center of the image matches the center of the screen it is rotate on that point. It important to say that the start of the image is considered the center of that image, and not the top-left corner of the image.
Changing the transformations for the image only (NOT Working!)
Switching order
I have tried switching the order of the concatenated matrices, in other words I first apply the transformation for the shape matrix m2 and then those from the matrix holding transformations for the whole scene m1.
var m = Matrix()
m.preConcat(m2)
m.preConcat(m1)
But now the opposite happens, it starts working correctly when changing the transformations for the shape, but it does not work when changing the transformations for the whole scene.
Changing the transformations for the whole scene, that is applied to all shapes (NOT Working!)
Changing the transformations for the image only (Working!)
Use different order depending on which matrix transformations we want to change
I have tried using different order when concatenating the matrix. The order is m1 -> m2 for the scene and m2 -> m1 for the image.
// use when transforming the scene
var m = Matrix()
m.preConcat(m1)
m.preConcat(m2)
// used when transforming the image
var m = Matrix()
m.preConcat(m2)
m.preConcat(m1)
Now it kinda works, the only problem is when I switch the order of the matrices I get a nasty translation as it can be seen at the 6-7 second. But other than that it works!
So there are few questions:
- What causes that translation, when changing the order of the matrices m1 and m2?
- Is there a way to solve the problem with the translation?
- Why the order of the matrices need to be changed?


