[Math] Find direction of angle between 2 vectors

vectors

I have successfully calculated the magnitude from one vector to another using:

$$\cos^{-1} \left(\frac{u \cdot v}{||u||\,||v||}\right).$$

However this does not tell me whether this angle is left or right, positive or negative.

I need this for a game I am creating in which I need to use where a ship is facing and where I want it to face (relative position of the destination) but I need to know if the ship needs to turn left or right.

How do I modify or extend this formula to add a direction to it?

Best Answer

I assume your game uses a top-down 2D perspective, so that you are storing your vectors $\vec{u}, \vec{v}$ as tuples

$\vec{u} = (u_1,u_2)$

$\vec{v} = (v_1,v_2)$

where $\vec{u}$ is the direction the ship is initially facing, and $\vec{v}$ is the direction you want it to face.

You can check to see if $\vec{v}$ lies to the left or to the right of $\vec{u}$ using the formula

$\delta = u_1 v_2 - u_2 v_1$

if $\delta$ is positive then $\vec{v}$ is to the left of $\vec{u}$, if $\delta$ is negative then $\vec{v}$ is to the right of $\vec{u}$. If $\delta$ is zero then $\vec{v}$ is in the opposite direction to $\vec{u}$.

Related Question