Getting angles for rotating $3$D vector to point in direction of another $3$D vector

I've been trying to solve this in Mathematica for $2$ hours, but got the wrong result.

I have a vector, in my case $\{0, 0, -1\}$. I want a function that, given a different vector, gives me angles DX and DY, so if I rotate the original vector by an angle of DX around the X axis, and then rotate it by an angle of DY around the Y axis, I'll get a vector with the same direction as the given vector.

(I want this so I could input angles to SolidWorks to rotate a part so it will satisfy a constraint I defined in Mathematica.)

• You're starting with a vector along the z axis. To get where you want to go, it's super easy to rotate first about the x-axis, and then about the z-axis. Is there some reason you're making it hard on yourself by rotating first about x, and then about y? – Marty Green Nov 23 '13 at 12:45
• An x-z answer is acceptable too. – Ram Rachum Nov 23 '13 at 16:30

So you need a 3×3 rotation matrix $E$ such that

$$E\,\begin{pmatrix} x \\ y \\ z \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ -1 \end{pmatrix} = -\hat{k}$$

This rotation matrix consists of two elementary rotations

\begin{aligned} E & = {\rm Rot}(\hat{i},\varphi_x){\rm Rot}(\hat{j},\varphi_y) \\ & = \begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos\varphi_x & -\sin\varphi_x \\ 0 & \sin\varphi_x & \cos\varphi_x \end{pmatrix} \begin{pmatrix} \cos\varphi_y &0 & \sin\varphi_y \\ 0 & 1 & 0 \\ - \sin\varphi_y & 0 & \cos\varphi_y \end{pmatrix} \end{aligned}

This is solved with

$$\varphi_x = - \tan^{-1} \left( \frac{y}{\sqrt{x^2+z^2}} \right) \\ \varphi_y = \pi - \tan^{-1} \left( \frac{x}{z} \right)$$

if $\sqrt{x^2+y^2+z^2}=1$ is true.

Verification

use $(x,y,z) = (\frac{1}{2}, \frac{3}{4}, \frac{\sqrt{3}}{4}) = (0.5, 0.75, 0.4330)$ to get

$$\varphi_x = - \tan^{-1} \left( \frac{\frac{3}{4}}{\sqrt{\left(\frac{1}{2}\right)^2+\left(\frac{\sqrt{3}}{4}\right)^2}} \right) = -0.8481 \\ \varphi_y = \pi - \tan^{-1} \left( \frac{\frac{1}{2}}{\frac{\sqrt{3}}{4}} \right) = 2.2845$$

With a rotation matrix

$$E = \begin{pmatrix} -0.6547 & 0 & 0.7559 \\ -0.5669 & 0.6614 & -0.4910 \\ -0.5 & -0.75 & -0.4330 \end{pmatrix}$$

and $$\begin{pmatrix} -0.6547 & 0 & 0.7559 \\ -0.5669 & 0.6614 & -0.4910 \\ -0.5 & -0.75 & -0.4330 \end{pmatrix} \begin{pmatrix} 0.5 \\ 0.75 \\ 0.4330 \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ -1\end{pmatrix}$$

NOTICE: Some combinations of $(x,y,z)$ will not yield the correct result because it won't be possible with the specific rotation sequence used.

• Thanks for the detailed answer! Two things: 1. I think you misunderstood me (my language was vague): I want to rotate the {0,0,-1} vector into the given vector, not the other way around. 2. I want to know how you did the "This is solved with" step. Where did arctan come from? I want to know how to make this step for future calculations. – Ram Rachum Nov 24 '13 at 13:03
• You know the $E^\top$ above will rotate the $\hat{k}$ vector into $(x,y,z)$. – ja72 Nov 24 '13 at 18:52
• What you are looking for are called Euler angles, and so search for "finding Euler angles" to get things like this paper which explains how to extract them for various rotation scenarios. – ja72 Nov 24 '13 at 18:56
• Also, to solve in your case you just create the system of equations from the rotation $\vec{v}=-E\hat{k}$ or in expanded form \begin{aligned} &x = -\sin\varphi_y \\ y &= \sin\varphi_x \cos\varphi_y \\ z &= -\cos\varphi_x \cos\varphi_y \end{aligned}. Divide the last two to get $\varphi_x$ and then combine them to get $$\sin\varphi_y = -x \\ \cos\varphi_y = \sqrt{y^2+z^2}$$ which is used to get the $\arctan$ values. I prefer those becasue the Atan2() function works miracles. – ja72 Nov 24 '13 at 19:05

Okay, if you don't mind rotating about the y and z axes, we can do it like this. Let's take a typical vector like (6,3,2) so the overall length is 7. You want to rotate a vector from the z axis into this orientation. First you look at the angle whose cosine is 2/7...that's how far down you have to come from the z axis. So you rotate about the y axis by that amount. Now you just have to swing around the z axis by the angle whose tangent is 3/6.

I think that's all you need to line up your vectors.