# How to rotate vector A around another vector B?

Lets say I know what is directly above a turret. I also know where the turrets gun is currently pointing. I would like to know where will the turret's gun will point if I rotate it.

I suppose I could multiply $$\A \cdot B\$$, project $$\A\$$ on the plane $$\B\$$ is a normal of and then do the rotation in 2D and add back $$\A \cdot B\$$ but I am sure the math should be simpler than that? You want to reflect $$\\vec A\$$ across $$\\vec B\$$. This has a simple formula, if $$\\vec B\$$ has length 1:

$$-\vec A + 2( \vec A \cdot \vec B)\vec B$$

What we've done here is to reverse $$\\vec A\$$ first, which means we negated both the component of $$\\vec A\$$ that's parallel to $$\\vec B\$$ and the component of $$\\vec A\$$ perpendicular to $$\\vec B\$$. Then we added twice the parallel component, $$\-1 + 2 = +1\$$, bringing it back to its previous value. So the net effect is that the part of $$\\vec A\$$ that was parallel to $$\\vec B\$$ remains unchanged, and only the perpendicular part gets reflected.

• It looks like this answer assumes the question is asking about 2D, but by my reading it's asking about 3D, and wants to rotate the turret of a 3D tank some angle about its turret rotation axis - not reflect the turret across that axis. Nov 10, 2020 at 17:16

You simply want to apply the rotation matrix.

Assuming you use a cartesian metric for your game, and your gun is pointing in direction $$\ \vec{A} = (x,y,z) \$$, and you rotate around the vertical axis $$\ \vec{B} = (0, 0, 1) \$$. Then the rotation $$\ R_z \$$ by and angle $$\ \theta \$$ is defined as $$R_z(\theta) = \left(\begin{array}{ccc}\cos \theta, & -\sin \theta,& 0\\\sin\theta, &\cos \theta,& 0\\0,&0,&0 \end{array}\right)$$

Thus the rotated vector $$\\vec{A}^\prime = \vec{A} \cdot R_z = (x\cos\theta-y\sin\theta, x\sin\theta + y\cos\theta, 0)\$$

For a more elaborate treatise around general axis, consult a book on algebra or have a look on the wikipedia article on rotation:

The matrix of a proper rotation $$\R\$$ by angle $$\\theta\$$ around the axis $$\\vec u =(u_{x},u_{y},u_{z})\$$, a unit vector with $$\u_{x}^{2}+u_{y}^{2}+u_{z}^{2}=1\$$, is given by:

$$\small {R = \begin{bmatrix}\cos \theta +u_{x}^{2}\left(1-\cos \theta \right)&u_{x}u_{y}\left(1-\cos \theta \right)-u_{z}\sin \theta &u_{x}u_{z}\left(1-\cos \theta \right)+u_{y}\sin \theta \\u_{y}u_{x}\left(1-\cos \theta \right)+u_{z}\sin \theta &\cos \theta +u_{y}^{2}\left(1-\cos \theta \right)&u_{y}u_{z}\left(1-\cos \theta \right)-u_{x}\sin \theta \\u_{z}u_{x}\left(1-\cos \theta \right)-u_{y}\sin \theta &u_{z}u_{y}\left(1-\cos \theta \right)+u_{x}\sin \theta &\cos \theta +u_{z}^{2}\left(1-\cos \theta \right)\end{bmatrix}}$$