I am trying to let the user of my app rotate a 3D object drawn in the center of the screen by dragging their finger on screen. A horizontal movement on screen means rotation around a fixed Y axis, and a vertical movement means rotation around the X axis. The problem I am having is that if I just allow rotation around one axis the object rotates fine, but as soon as I introduce a second rotation the object doesn't rotate as expected.
Here is a picture of what is happening:
The blue axis represents my fixed axis. Picture the screen having this fixed blue axis. This is what I want the object to rotate in relation to. What is happening is in red.
Here's what I know:
The first rotation around Y (0, 1, 0) causes the model to move from the blue space (call this space A) into another space (call this space B) Trying to rotate again using the vector (1, 0, 0) rotates around the x axis in space B NOT in space A which is not what I mean to do.
Here's what I tried to fix this, given what I (think) I know (leaving out the W coord for brevity):
- First rotate around Y (0, 1, 0) using a Quaternion.
- Convert the rotation Y Quaternion to a Matrix.
- Multiply the inverse of the Y rotation matrix by my fixed axis x Vector (1, 0, 0) to get the fixed X axis in relation to the new space.
- Rotate around this new X Vector using a Quaternion.
This isn't working how I expect. The rotation seems to work, but at some point horizontal movement doesn't rotate about the Y axis, it appears to rotate about the Z axis.
I'm not sure if my understanding is wrong, or if something else is causing a problem. I have some other transformations I'm doing to the object besides rotation. I move the object to the center before applying rotation. I rotate it using the matrix returned from my function above, then I translate it -2 in the Z direction so I can see the object.