Translation after rotation

I’m using OpenGL ES 2.0 for Android. I’m translating and rotating a model using the touch screen. My translations are only in the (x, y) plane, and my rotation is only about the z-axis. Imagine looking directly down at a map on a table and moving to various coordinates on the map, and being able to rotate the map around the point you are looking at.

The problem is that after I rotate, my subsequent translations are to longer matched to the motions of the pointer on the screen, the axes are different.

Everything I’ve tried gives me one of two behaviors. One is equivalent to:


Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, Xposition, Yposition, 0.0f);
Matrix.rotateM(mModelMatrix, 0, rotationAngle, 0.0f, 0.0f, 1.0f);

This allows me to translate as expected (up/down on the screen moves the model up and down, left/right moves model left and right), regardless of rotation. The problem is that the rotation is about the center of the object, and I need the rotation to be about the point that I am looking at, which is different than the center of the object.

The other behavior I can get is equivalent to:


Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, rotationAngle, 0.0f, 0.0f, 1.0f);
Matrix.translateM(mModelMatrix, 0, Xposition, Yposition, 0.0f);

This gives me the rotation that I want, always about the point I am looking at. The problem is that after a rotation, the translations are wrong. Left/right on the screen translates the object at a different angle, along the rotated axes.

I need some way to get both behaviors at the same time. It needs to rotate about the point I am looking at, and translate in the direction that the finger moves on the screen.

In summary:

I have an object that starts at (0, 0, 0) in world coordinates. My view is looking down the z-axis at the object, and I want to limit translation to the x/y plane. I also want to rotate the object about the z-axis only. The center of the rotation must always be the center of the screen.

I am controlling the translation with the touch screen so I need the object to the same way the finger moves, regardless of how it it rotated.

I think for the first case you need to also rotate (Xposition,Yposition,0) so that your screen coordinates rotate at the same time as you rotate around the ZAxis.

Your assumption on the difference between the 2 is correct though, the first rotates around the point where the camera is and the 2nd rotates around the (0,0,0)

I think the second approach is easier to modify to give you the desired behavior. When the user translates, try applying the transposed rotation matrix to the incremental translation vector before adding it to your total translation vector. Say if your current translation vector is (Xposition, Yposition), your current rotation matrix mRotation, and the user translates by (XIncr, yIncr), you would update the translation vector as:
(Xposition, Yposition) = (Xposition, Yposition) + mRotation.transposed() * (XIncr, YIncr)

Many thanks reto.koradi, that worked for me.