# How to rotate cuboid to plane

I have a cuboid with 8 points that is axis aligned with its center at the origin 0,0,0. Now I have a plane and want my cuboid to rotate so that instead of being axis aligned, it is now aligned to this plane and its front and back side are parallel to the plane and the center is on the plane.

However, I also want to get the euler angles for this rotation so I can manually change the result. It needs to be euler angles and not a matrix/quaternion so the values can be changed by the user and euler angles are the best way for this representation.

I tried some ways to do this, but it never accurately worked.

Compute three vectors $X$, $Y$, $Z$ representing your new coordinate frame as you describe it. $Z$ would be the normal to your plane, and $X$ and $Y$ are determined by your "its front and back side are parallel."

Then follow the description in Wikipedia, Geometric derivation, which details the computation of the Euler angles from $\{X,Y,Z\}$.

(Image from Wikipedia.)

• I don't get the text on the wikipedia link. As you said I have my xyz vectors of the old frame and XYZ of the new coordinate frame. You can see the vectors z1 z2 z3 on the wiki site i.stack.imgur.com/j2SOK.png They seem to be equal to the xyz vectors?! Then in the formulas, they use $X_3$, $Y_3$, $Z_1$, $Z_2$ and $Z_3$ to compute the three angles. But where do these vectors come from? I can't see them in the pictures, those only show lowercase XYZ.
– RBS
Jul 14, 2015 at 13:47
• @RBS: $Z_1, Z_2, Z_3$ are as illustrated in the figure. For example, $Z_3$ can be found as $Z \cdot z$, assuming both unit vectors. Then $\beta = \cos^{-1} Z_3$. Etc. Jul 14, 2015 at 14:44
• @RBS: Oh, maybe it's not clear that $Z_1,Z_2,Z_3$ are not vectors, but rather reals, or distances: projections onto $x,y,z$ unit vectors. Jul 14, 2015 at 17:44

Find the two rotation angles α and β for the given plane using the link above and then apply the rotation to all the nodes in the mesh.

The direction of the plane is described as two rotations about the X and Y axis. The details I think are as follows:

$$\left. \frac{(a,b,c)}{|(a,b,c)|} = {\rm RY}(\alpha) {\rm RX}(-\beta) \hat{k} \right\}$$

\begin{align} \frac{a}{\sqrt{a^2+b^2+c^2}} &= \sin(\alpha)\cos(\beta) \\ \frac{b}{\sqrt{a^2+b^2+c^2}} &= \sin(\beta) \\ \frac{c}{\sqrt{a^2+b^2+c^2}}&= \cos(\alpha)\cos(\beta) \end{align}

The above is solved for $\sin(\beta)$ from the 2nd equation, and for $\cos(\beta)$ by squaring and adding the 1st and 3rd equation.

$$\frac{a^2+b^2}{a^2+b^2+c^2} = \cos^2(\beta)$$

Then $\cos(\beta)$ is substituted into the 1st and 3rd equation to be solved for $\sin(\alpha)$ and $\cos(\alpha)$