Game Designer & Developer


Game Dev

Find Angle Between Two Vectors

Calculate the angle between two vectors using ArcCos or Diamond Angle

Using ArcCos

θ=arccos(ABA×B)θ = \arccos{\left(\frac{\vec{A}\cdot\vec{B}}{\left|\vec{A}\right|\times\left|\vec{B}\right|}\right)}

For normalised vectors it is simply the arccos\arccos of the dot product as the divisor will be 11.

This is the reverse of Dot Product

Using Diamond Angle

First calculate the diamond angle of both vectors, see Diamond Angle, and then just subtract the angle of one vector from the angle of another.

Code

Example Python code (requires the diamond_angle code from Diamond Angle).

def angle_between(x1, y1, x2, y2):
  a1 = diamond_angle(x1, y1)
  a2 = diamond_angle(x2, y2)
  return a2-a1