How to calculate rotation of arrow to point along a vector in 3D?

500 Views Asked by At

I have a 3D vector Starting at some point A and ending at some point B. I have an arrow sitting at point A. I need to rotate the arrow such that it is pointing along the vector from point A to point B. The arrow starts pointing along the y axis (take a look at the image to see this better). I'm really not much of a mathematician and I'm struggling with this. Any help would be much appreciated.

Vector AB in the 3D space

2

There are 2 best solutions below

3
On

So you need three things:

  1. normal vector using cross product
  2. angle between vectors
  3. a matrix that rotates the arrow

So the first step is simply cross product. 2nd step is angle between vectors using $$ a=\frac{acos(a_1 \cdot a_2)}{len(a_1)*len(a_2)} $$, where $len(a_0) = \sqrt{a_x^2+a_y^2+a_z^2}$

3rd step is a matrix: \begin{bmatrix} cos(a)+(1-cos(a))*x^2 & (1-cos(a))*x*y-sin(a)*z & (1-cos(a))*x*z+sin(a)*y & 0 \\ (1-cos(a))*y*x+sin(a)*z & cos(a)+(1-cos(a))*y^2 & (1-cos(a))*y*z-sin(a)*x & 0 \\ (1-cos(a))*z*x-sin(a)*y & (1-cos(a))*z*y+sin(a)*x & cos(a)+(1-cos(a))*z^2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} There $(x,y,z) = normalize(cross(a_1,a_2))$.

Note that you can only apply the matrix when the vectors start from origo, so you need translate the system from point A to origo using standard translate matrix. and then after applying the matrix, translate it back to point A.

4
On

Let's call your first vector--the one from A to B-- $\vec x=\langle x_1,x_2,x_3\rangle$. Let us call the other vector $\vec y=\langle y_1,y_2,y_3\rangle$. We follow these steps:

  1. Find the angle between $\vec x$ and $\vec y$. To calculate the angle between vectors, use the dot product. The dot product has the property that $\vec x\cdot\vec y=\Vert \vec x\Vert \Vert \vec y\Vert\cos\theta$, where $\theta$ is the angle between vectors. Translated to vector components, this means $$x_1y_1+x_2y_2+x_3y_3=\sqrt{(x_1^2+x_2^2+x_3^2)(y_1^2+y_2^2+y_3^2)}\cos\theta.$$ This allows you to calculate the angle $$\theta=\cos^{-1}\left(\frac{x_1y_1+x_2y_2+x_3y_3}{\sqrt{(x_1^2+x_2^2+x_3^2)(y_1^2+y_2^2+y_3^2)}}\right).$$

  2. Find the direction of the axis of rotation. The axis of rotation is perpendicular to both of the vectors. To find a vector in that direction, use the cross product. The cross product is $\vec x\times\vec y=\langle x_2y_3-x_3y_2,x_3y_1-x_1y_3,x_1y_2-x_2y_1\rangle$. Let $z_1=x_2y_3-x_3y_2$, $z_2=x_3y_1-x_1y_3$, and $z_3=x_1y_2-x_2y_1$, with $\vec z=\vec x\times\vec y=\langle z_1,z_2,z_3\rangle.$ The next step will be easier if the vector pointing in the direction of the axis of rotation has length one. To this end, let $$\vec w=\langle\vec w_1,w_2,w_3\rangle=\left\langle \frac{z_1}{\sqrt{z_1^2+z_2^2+z_3^2}},\frac{z_2}{\sqrt{z_1^2+z_2^2+z_3^2}},\frac{z_3}{\sqrt{z_1^2+z_2^2+z_3^2}}\right\rangle.$$ $\vec w$ is also perpendicular to both $\vec x$ and $\vec y$, but has length $1$.

  3. Use Rodrigues' Formula. Rodrigues' Formula tells us the components of the rotated vector based on $\vec w$ and $\theta$, and it uses the dot product and cross product again. It is: \begin{align*}\text{rotated vector}&=\vec y \cos\theta+(\vec w\times \vec y)\sin\theta+\vec w(\vec w\cdot\vec y)(1-\cos\theta)\\&=\langle y_1 \cos (\theta )-w_2 y_3 \sin (\theta )+w_3 y_2 \sin (\theta ),\\&w_1 y_3 \sin (\theta )-w_3 y_1 \sin (\theta )+y_2 \cos (\theta ),\\&-w_1 y_2 \sin (\theta )+w_2 y_1 \sin (\theta )+y_3 \cos (\theta )\rangle.\end{align*}

It is pretty complicated, but that is the simplest way to do it.

Edit: since $\vec w$ is perpendicular to $\vec y$, their dot product is zero, and so the result of Rodrigues' Formula is simpler. I changed it above.