# Matrix of transform rotation [solved]

Im trying to create matrix which rotates vector. I have $\vec{g}=(g_1,g_2,g_3);\:g_1\in\mathbb{R},g_2\in\mathbb{R},g_3\in\mathbb{R}$ - it represents gravitation. And $\vec{o}=(o_1,o_2,o_3)$ is vector that gravitation should be transform in so: $\vec{o}=(|\vec{g}|,0,0)$.

So rotation angle is: $\cos\alpha=\frac{\vec{\mathbf{g}}\cdot \vec{\mathbf{o}}}{|\vec{\mathbf{g}}|\cdot|\vec{\mathbf{o}}|}=\frac{g_1o_1+g_2o_2+g_3o_3}{\sqrt{g_1^2+g_2^2+g_3^2}\cdot\sqrt{o_1^2+o_2^2+o_3^2}}$

Rotation axis is $\vec{r}=\vec{g}\times\vec{o}=[g_2o_3 - o_2g_3,g_3o_1 - o_3g_1,g_1o_2 - o_1g_2]$

Then unit vector computed from rotation axis $\vec{n}=(\frac{r_1}{|\vec{r}|},\frac{r_2}{|\vec{r}|},\frac{r_3}{|\vec{r}|})$

In the end rotation matrix should be:

$R=\begin{bmatrix} \cos \alpha +n_1^2 \left(1-\cos \alpha\right) & n_1 n_2 \left(1-\cos \alpha\right) - n_3 \sin \alpha & n_1 n_3 \left(1-\cos \alpha\right) + n_2 \sin \alpha \\ n_2 n_1 \left(1-\cos \alpha\right) + n_3 \sin \alpha & \cos \alpha + n_2^2\left(1-\cos \alpha\right) & n_2 n_3 \left(1-\cos \alpha\right) - n_1 \sin \alpha \\ n_3 n_1 \left(1-\cos \alpha\right) - n_2 \sin \alpha & n_3 n_2 \left(1-\cos \alpha\right) + n_1 \sin \alpha & \cos \alpha + n_3^2\left(1-\cos \alpha\right) \end{bmatrix}$

Now I can rotate my vector A

$B = A \times R$

EDIT: I meant: $B= RA$

But I implemented it and it doesn't work. What is wrong?

EDIT2: In the end it was a mistake in my source code. It works good.

-

I don't know what is your "vector $A$", but a 3D rotation of a 3D vector cannot be applied by postmultiplying, i.e., $AR$ is wrong if $A$ is a (column) vector.

What you are looking for is a simple case of a QR factorization. This can be done with two consecutive Givens rotations. Check out this second link; it explains how to compute these.

The idea is to do this:

$$g = \begin{bmatrix} g_1 \\ g_2 \\ g_3 \end{bmatrix} = Q_1 \begin{bmatrix} \sqrt{g_1^2 + g_3^2} \\ g_2 \\ 0 \end{bmatrix} = Q_1 Q_2 \begin{bmatrix} \sqrt{g_1^2 + g_2^2 + g_3^2} \\ 0 \\ 0 \end{bmatrix}.$$

This computation avoids the computation of a rotation angle. Instead, it works only with tangent and its accompanying sine and cosine (computed directly from tangent). This is both fast and, if done right, numerically stable.

Alternatively, this can be also be done through a single Householder transform, but this may seem a bit counterintuitive, since you want a rotation.

-
en.wikipedia.org/wiki/… Why this doesnt work? –  SpeedEX505 Nov 27 '13 at 16:20
@SpeedEX505 It should work. Check that you implemented it properly and that you're premultiplying your vector (not postmultiplying). If you can't find a bug in your code, maybe folks at StackOverflow can help. –  Vedran Šego Nov 27 '13 at 21:22
Yes it works. I find a mistake in my code. Thanks for help. –  SpeedEX505 Nov 28 '13 at 13:37

It worked well. I had an error in my source code.

-