# Rotate object around a fixed coordinate axis

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):

1. First rotate around Y (0, 1, 0) using a Quaternion.
2. Convert the rotation Y Quaternion to a Matrix.
3. 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.
4. 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.

• Can you be more specific about how you approach the problem? What exactly are you using? – Olivier Mar 13 '15 at 19:14
• @Olivier I posted a more detailed version of the question here – Christopher Perry Mar 13 '15 at 19:18
• Has it not been answered? – Olivier Mar 13 '15 at 19:19
• Nope. I've asked on Reddit, Unity Answers, Game Dev stack, and here. No answer, at least not an acceptable one that actually answers my question. – Christopher Perry Mar 13 '15 at 19:20
• Your nice animation shows 3 rotations, with 9 vectors. Which are X and Y? You should be able to just keep multiplying the object's current quaternion by the new finger-swipe quaternion, and it should work. The problem is unlikely to be in your other transformations. It looks like using a quaternion for step 1 is a waste of time since you could directly get the result of step 2 with a sin and cos. Then steps 3 and 4 seem to be the source of your problem -- rotating around the rotated X axis (red) is exactly what you don't like. Just rotate around the standard X axis (blue). – Matt Mar 18 '15 at 1:08

## 1 Answer

I think your problem is related to frame of reference. Your global frame is A and other frames obtained after rotation in A are local frames (such as B). So u want all your rotations in global frame but they are taking place in local frame. For rotation in global frame of reference you need to pre-multiply the transformation matrix and for rotation in local frame, post-multiply. For eg, if u want rotation by $\theta_1$ in A for which transformation matrix is $R_{\theta_1}$ and then rotation by $\theta_2$ in A (rotation matrix $R_{\theta_2}$), then: $$V_f = R_{\theta_2}*R_{\theta_1}*V_i$$ where $V_i$ is initial position of the point(or object) and $V_f$ is final. for further reference, check this link: Maths - frame-of-reference for combining rotations