How to get an angle between 2 Vectors?

For me I was trying to find the signed angle between two vectors, which meant finding the angle between two vectors that could rotate a full 360 degrees, but be represented as -180 to +180.

This worked for me:

1) Normalize both your vectors. The vector A is “From” and the vector B is “To”. We are getting the angle “From” Vector A “To” vector B. Note that I lowered the tolerance here to the minimum for greater accuracy.

2) Take the dot product of the two normalized vectors and get the arccos of that value. This is our angle

3) Take the cross product of the two normalized vectors. This produces a perpendicular vector to Vector A and Vector B. We need this vector because we need to define the axis on which we test what will be negative rotation and what will be positive. In my example I have used X, because I care about the Roll. But in your case it may be the Pitch or Yaw that you care about. It creates a new axis relative to the A and B which we can use to define Relative Rotation.

4) If the Cross Product’s vector axis ranges from 0 to -1, that is negative rotation, while 0 to +1 means positive rotation. -1 means a full -180 degrees, 0 being 0 rotation on the X axis, and +1 means a full 180 degrees.

340633-vectors.png

3 Likes