Guest User

Untitled

a guest
Sep 28th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1.     void Update()
  2.     {
  3.         Quaternion swing, twist;
  4.  
  5.         DecomposeQuat (transform.localRotation, Vector3.forward, out swing, out twist);
  6.  
  7.         float swing_angle, twist_angle;
  8.         Vector3 swing_axis, twist_axis;
  9.  
  10.         swing.ToAngleAxis (out swing_angle, out swing_axis);
  11.         twist.ToAngleAxis (out twist_angle, out twist_axis);
  12.  
  13.         swing_angle = Mathf.Clamp (swing_angle, 45.0F, 90.0F);
  14.         twist_angle = Mathf.Clamp (twist_angle, 45.0F, 90.0F);
  15.  
  16.         transform.localRotation = Quaternion.AngleAxis (swing_angle, swing_axis) * Quaternion.AngleAxis (twist_angle, twist_axis);
  17.     }
  18.  
  19.     private Quaternion Normalize(Quaternion q)
  20.     {
  21.         float dot = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
  22.         float invLength = 1.0F / Mathf.Sqrt (dot);
  23.  
  24.         return new Quaternion (q.x * invLength, q.y * invLength, q.z * invLength, q.w * invLength);
  25.     }
  26.  
  27.     private void DecomposeQuat(Quaternion rotation, Vector3 direction, out Quaternion swing, out Quaternion twist)
  28.     {
  29.         Vector3 v = new Vector3 (rotation.x, rotation.y, rotation.z);
  30.         Vector3 p = Vector3.Project (v, direction);
  31.  
  32.         twist = Normalize(new Quaternion (p.x, p.y, p.z, rotation.w));
  33.         swing = rotation * Quaternion.Inverse (twist);
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment