Signed angle between two 3D vectors with same origin within the same plane

3d, geometry, math, vector

Solution

The solution I'm currently using seems to be missing here. Assuming that the plane normal is normalized (`|Vn| == 1`), the signed angle is simply:

For the right-handed rotation from Va to Vb:

`atan2((Va x Vb) . Vn, Va . Vb)`

For the left-handed rotation from Va to Vb:

`atan2((Vb x Va) . Vn, Va . Vb)`

which returns an angle in the range [-PI, +PI] (or whatever the available atan2 implementation returns).

`.` and `x` are the dot and cross product respectively.

No explicit branching and no division/vector length calculation is necessary.

Explanation for why this works: let alpha be the direct angle between the vectors (0° to 180°) and beta the angle we are looking for (0° to 360°) with `beta == alpha` or `beta == 360° - alpha`

Va . Vb == |Va| * |Vb| * cos(alpha)    (by definition) 
        == |Va| * |Vb| * cos(beta)     (cos(alpha) == cos(-alpha) == cos(360° - alpha)


Va x Vb == |Va| * |Vb| * sin(alpha) * n1  
    (by definition; n1 is a unit vector perpendicular to Va and Vb with 
     orientation matching the right-hand rule)

Therefore (again assuming Vn is normalized):
   n1 . Vn == 1 when beta < 180
   n1 . Vn == -1 when beta > 180

==>  (Va x Vb) . Vn == |Va| * |Vb| * sin(beta)

Finally

tan(beta) = sin(beta) / cos(beta) == ((Va x Vb) . Vn) / (Va . Vb)

Problem

What I need is a signed angle of rotation between two vectors Va and Vb lying within the same 3D plane and having the same origin knowing that: - The plane contatining both vectors is an arbitrary and is not parallel to XY or any other of cardinal planes - Vn - is a plane normal - Both vectors along with the normal have the same origin O = { 0, 0, 0 } - Va - is a reference for measuring the left handed rotation at Vn The angle should be measured in such a way so if the plane would be XY plane the Va would stand for X axis unit vector of it. I guess I should perform a kind of coordinate space transformation by using the Va as the X-axis and the cross product of Vb and Vn as the Y-axis and then just using some 2d method like with atan2() or something. Any ideas? Formulas?

Original source