# Rotation about arbitrary point and arbitrary axis

This should be a simple problem, but I appear to not be able to get this correct. I have an object that is rotating in a circle on the $x$-$y$ plane (rotating in the $-z$ direction) at a speed $\dot{\gamma}$. This object is also rotating about its own axis at a speed $\dot{\theta}$. Given an initial set of coordinates for the object, I need to find the position of the object at a given $\Delta t$ later. Here is a (terrible) figure:

Starting in the Cartesian coordinate frame, I first move the object to the origin by translating by the vector $[-r \cos(\phi),r \sin(\phi),0]$. I then rotate the object in the $z$ axis by $\phi$, by

$$\begin{bmatrix} \cos(\phi) && -\sin(\phi) && 0 \\ \sin(\phi) && \cos(\phi) && 0 \\ 0 && 0 && 1 \end{bmatrix} \begin{bmatrix} x - r \cos(\phi) \\ y + r \sin(\phi) \\ z \end{bmatrix}=\begin{bmatrix} x \cos(\phi)-y \sin(\phi)-r \\ x \sin(\phi)+y \cos(\phi) \\ z \end{bmatrix}$$

At this point, the object is now in this configuration:

Now, rotating about its own axis is simply a rotation in the $y$-axis by an angle $\theta =\dot{\theta} \Delta t$.

$$\begin{bmatrix} \cos(\theta) && 0 && -\sin(\theta) \\ 0 && 1 && 0 \\ \sin(\theta) && 0 && \cos(\theta) \end{bmatrix} \begin{bmatrix} x \cos(\phi) - y \sin(\phi) - r \\ x \sin(\phi) + y \cos(\phi) \\ z \end{bmatrix} = \\ \begin{bmatrix} \left( x\cos(\phi) - y \sin(\phi) - r \right) \cos(\theta) - z \sin(\theta) \\ x \sin(\phi)+y \cos(\phi) \\ \left( x \cos(\phi) - y \sin(\phi) - r \right) \sin(\theta) + z \cos(\theta) \end{bmatrix}$$

I now rotate by $-\phi$ to get the object in the original frame.

$$\begin{bmatrix} \cos(\phi) && \sin(\phi) && 0 \\ -\sin(\phi) && \cos(\phi) && 0 \\ 0 && 0 && 1 \end{bmatrix} \begin{bmatrix} \left( x\cos(\phi) - y \sin(\phi) - r \right) \cos(\theta) - z \sin(\theta) \\ x \sin(\phi)+y \cos(\phi) \\ \left( x \cos(\phi) - y \sin(\phi) - r \right) \sin(\theta) + z \cos(\theta) \end{bmatrix} = \\ \begin{bmatrix} \left[ \left( x\cos(\phi) - y \sin(\phi) - r \right) \cos(\theta) - z \sin(\theta) \right] \cos(\phi)+\left( x \sin(\phi) + y \cos(\phi) \right) \sin(\phi) \\ -\left[ \left( x\cos(\phi) - y \sin(\phi) - r \right) \cos(\theta) - z \sin(\theta) \right] \sin(\phi) + \left( x \sin(\phi) + y \cos(\phi) \right) \cos(\phi) \\ \left( x \cos(\phi) - y \sin(\phi) - r \right) \sin(\theta) + z \cos(\theta) \end{bmatrix}$$

This is then simply translated back to the original location by the vector $\left[ r \cos(\phi), -r \sin(\phi), 0 \right]$. Finally, in order to rotate around the circle itself by $\gamma = \dot{\gamma} \Delta t$ about the $-z$ axis, which I do by simply converting the coordinates to cylindrical, and subtracting $\gamma$ from the $\theta$-coordinate. Using this formulation, I do not get the right result (this is used in a code that uses this information to generate a surface geometry); presumably I have made an algebraic error somewhere (I have checked the code several times, there is no transcription error between the code and the algebra).