# Rotate two vectors until one of them is “upright”

I've have two vectors A and B. I want to rotate both of them by the same angle until A is "upright" (has only a positive y component). Is it possible to calculate B without using functions like sin or tan?

I'm currently working on a game which solves this by calculating A's angle and rotating B by this angle. I hoped that there would be a more efficient way to do this to save some precious CPU-time.

Is this even possible? Or am I just to blind to see the obvious solution?

Thank you :-) Marek

edit: It's a planar problem. I've tried to illustrate it:

-
Wait, is this in the plane or in space? Could you probably draw a picture that demonstrates what you want to happen? –  Guess who it is. Sep 24 '11 at 10:47
Well yeah. Just replace the $\sin$ and $\cos$ in the rotation matrix with the more direct computations: $$\cos\theta=\frac{\vec{A}\cdot\vec{e}_2}{\|\vec{A}\|},\quad\sin\theta=\frac{( \vec{A}\times\vec{e}_2)\cdot\vec{e}_3}{\|\vec{A}\|}$$ –  anon Sep 24 '11 at 11:24
Ah, well: $$\frac1{\sqrt{a_1^2+a_2^2}}\begin{pmatrix}a_2&-a_1\\a_1&a_2\end{pmatrix}$$ is the matrix you need... –  Guess who it is. Sep 24 '11 at 12:18
Thanks anon, this is basically what I was looking for but now I struggling with solving this equations (I'm sorry, I seem to have forgotten all of my vector knowledge). I know that $\|\vec{A}\|=\sqrt{(A_1)^2+(A_2)^2}$ but how do I calculate $\vec{A}\cdot\vec{e}_2$ or $(\vec{A}\times\vec{e}_2)\cdot\vec{e}_3$. More exactly: What is $\vec{e}_n$? I'm sorry to brother you with such basic questions :( –  marekventur Sep 24 '11 at 12:35
Oh, thank you J.M. This seems to be exactly what I was looking for. I'm going to try this out. –  marekventur Sep 24 '11 at 12:38

If $T$ rotates the plane by an angle $\theta$ in the counter-clockwise direction then its matrix is $$\left[\matrix{c & -s \cr s& c \cr}\right]\ ,$$ where $c:=\cos\theta$ and $s:=\sin\theta$. Now you want $T$ such that $$\left[\matrix{c & -s \cr s& c \cr}\right]\left[\matrix{a_1 \cr a_2 \cr}\right]\ =\ \left[\matrix{0 \cr \sqrt{a_1^2+a_2^2} \cr}\right]\ .$$ Solving this for $c$ and $s$ one obtains $$c={a_2\over \sqrt{a_1^2+a_2^2}}\ ,\quad s={a_1\over\sqrt{a_1^2+a_2^2}}\ .$$ The coordinates of the point $b':=T\> b$ are then given by $$\left[\matrix{b_1' \cr b_2' \cr}\right]=\left[\matrix{c & -s \cr s& c \cr}\right]\ \left[\matrix{b_1 \cr b_2 \cr}\right]\ .$$