Advertisement
Ckef01

Quaternion from and to Angle/Axis

Aug 1st, 2012
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. quaternion &quaternion::fromAngleAxis(const float angle, const vec3 &axis)
  3. {
  4.     float halfAngle = angle * 0.5f;
  5.     float scale = sin(halfAngle) / axis.magnitude();
  6.  
  7.     x = axis.x * scale;
  8.     y = axis.y * scale;
  9.     z = axis.z * scale;
  10.     w = cos(halfAngle);
  11.  
  12.     return *this;
  13. }
  14.  
  15. //-----------------------------------------------------------------------
  16. void quaternion::toAngleAxis(float &angle, vec3 &axis) const
  17. {
  18.     float halfAngle = acos(w);
  19.     float scale = 1.0f / sin(halfAngle);
  20.  
  21.     angle = halfAngle * 2.0f;
  22.     axis.x = x * scale;
  23.     axis.y = y * scale;
  24.     axis.z = z * scale;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement