Simplified problem presentation
I am trying to describe a specific motion from a point of view of a moving coordinate system. To make easier the comprehension I created this childish animation, where Mickey Mouse's face (expressed in the local coordinate system) is moving from left to right and the observer is looking directly at Mickey Mouse's face all the time (red coordinate system).

Geometrically the problem is described in the following image:

- $\vec{r_\ell}$ and $\beta \,$ (or $x_{A,\ell}$ and $y_{A,\ell}$) are the data holders of Mickey Mouse's face contour
- $\psi$ is the angle parameter
- $R$ is the radius of the circle
- $A$ is a specific point of the Mickey Mouse's face
The objective is to determine $\left[ x, \, y \right]$ in the rotating coordinate system. According to the image the equations should be:
- $\quad x = | \vec{r_\ell} | \, \cos{ \left( \beta + \psi \right)}$
- $\quad y = \frac{R}{\cos{\psi}} + | \vec{r_\ell} | \, \sin{ \left( \beta + \psi \right)}$
Real problem presentation
The real issue is involved in gear engineering, but I do not think it really matters, since it is a pure kinematic problem. Basically I want this animation with respect to the rotating coordinate system of the cogwheel.
I tried several times, but I am always getting the same wrong result. I also tried with the rotating transformation matrix: $T = \left[ \cos{\left(-\psi \right)} \ \sin{\left(-\psi \right)}; \ -\sin{\left(-\psi \right)} \ \cos{\left(-\psi \right)} \right]$ but I obtained the same problematic areas represented in the following animation and image:
The gear rack goes always over the cogwheel profile, which shall not do it. It should essentially roll as in the previous image, where both parts are moving. I really have no idea, why this is happening, so every single helpful advice is appreciated.



