Advertisement
Guest User

Angle3dVectors

a guest
Mar 9th, 2021
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. The solution I'm currently using seems to be missing here.
  2. Assuming that the plane normal is normalized (`|Vn| == 1`), the signed angle is simply:
  3.  
  4. For the right-handed rotation from Va to Vb:
  5.  
  6. `atan2((Va x Vb) . Vn, Va . Vb)`
  7.  
  8. For the left-handed rotation from Va to Vb:
  9.  
  10. `atan2((Vb x Va) . Vn, Va . Vb)`
  11.  
  12. which returns an angle in the range [-PI, +PI] (or whatever the available atan2 implementation returns).
  13.  
  14. `.` and `x` are the dot and cross product respectively.
  15.  
  16. No explicit branching and no division/vector length calculation is necessary.
  17.  
  18. 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`
  19.  
  20. (1) Va . Vb == |Va| * |Vb| * cos(alpha) (by definition)
  21. (2) == |Va| * |Vb| * cos(beta) (cos(alpha) == cos(-alpha) == cos(360° - alpha)
  22.  
  23.  
  24. (3) Va x Vb == |Va| * |Vb| * sin(alpha) * n1
  25. (by definition; n1 is a unit vector perpendicular to Va and Vb with
  26. orientation matching the right-hand rule)
  27.  
  28. Therefore (again assuming Vn is normalized):
  29. (4) n1 . Vn == 1 when beta < 180
  30. (5) n1 . Vn == -1 when beta > 180
  31.  
  32. So when beta < 180:
  33. (6) sin(alpha) * n1 . Vn == sin(alpha) == sin(beta)
  34. Otherwise:
  35. (7) sin(alpha) * n1 . Vn == -sin(alpha) == sin(beta)
  36.  
  37. From (3), we have:
  38. (8) (Va x Vb) . Vn == |Va| * |Vb| * sin(alpha) * n1 . Vn
  39.  
  40. And by plugging (6) and (7) into (8), we get:
  41.  
  42. (9) (Va x Vb) . Vn == |Va| * |Vb| * sin(beta)
  43.  
  44. Finally
  45.  
  46. tan(beta) = sin(beta) / cos(beta) == ((Va x Vb) . Vn) / (Va . Vb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement