# How to calculate the rotation of an object wrt container, which itself is rotated wrt universe, so that the object has nil rotation wrt universe?

Long subject, so I am hoping that explains what I am asking, but still let me elaborate with an example.

Let us assume we have an object as part of a container. Now the container is rotated by certain ROTATIONX, ROTATIONY, ROTATIONZ wrt the universe. Now we need to rotate the object WRT the container by rotationx, rotationy, rotationz such that the rotation of the object WRT the universe (rotx, roty, rotz) is zero. How do we do that?

-

There is theorem stating that in 3 dimensions, the composition of any two rotations in 3d-dimensional space is another rotations. This allows you to solve your problem as follows.

Suppose that from knowing the coordinates of the rotated container, and the coordinates of the container before rotations, you can represent $R=R_z\circ R_y\circ R_x$ by a matrix $$R=\left[\begin{matrix}a&d&g\\b&e&h\\c&f&i\end{matrix}\right]$$ where $R(1,0,0)=(a,b,c)$, $R(0,1,0)=(d,e,f)$ and $R(0,0,1)=(g,h,i)$.

Then the inverse rotation of $R$ is, in matrix form:

$$R^T=\left[\begin{matrix}a&b&c\\d&e&f\\g&h&i\end{matrix}\right]$$

So all you need is an algorithm that decomposes the matrix $R^T$ into its constituent rotations around the $x$-, $y$-, and $z$-axis. A good write-up I found via google is this blog post, along with actual computer code!.

-