/Scripts/MathUtils.cs

http://acid-and-base.googlecode.com/ · C# · 157 lines · 116 code · 27 blank · 14 comment · 7 complexity · a4dc6f168596d7a670ef438dec315909 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class MathUtils
  5. {
  6. public static float GetQuatLength(Quaternion q)
  7. {
  8. return Mathf.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
  9. }
  10. public static Quaternion GetQuatConjugate(Quaternion q)
  11. {
  12. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  13. }
  14. /// <summary>
  15. /// Logarithm of a unit quaternion. The result is not necessary a unit quaternion.
  16. /// </summary>
  17. public static Quaternion GetQuatLog(Quaternion q)
  18. {
  19. Quaternion res = q;
  20. res.w = 0;
  21. if (Mathf.Abs(q.w) < 1.0f)
  22. {
  23. float theta = Mathf.Acos(q.w);
  24. float sin_theta = Mathf.Sin(theta);
  25. if (Mathf.Abs(sin_theta) > 0.0001)
  26. {
  27. float coef = theta / sin_theta;
  28. res.x = q.x * coef;
  29. res.y = q.y * coef;
  30. res.z = q.z * coef;
  31. }
  32. }
  33. return res;
  34. }
  35. public static Quaternion GetQuatExp(Quaternion q)
  36. {
  37. Quaternion res = q;
  38. float fAngle = Mathf.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z);
  39. float fSin = Mathf.Sin(fAngle);
  40. res.w = Mathf.Cos(fAngle);
  41. if (Mathf.Abs(fSin) > 0.0001)
  42. {
  43. float coef = fSin / fAngle;
  44. res.x = coef * q.x;
  45. res.y = coef * q.y;
  46. res.z = coef * q.z;
  47. }
  48. return res;
  49. }
  50. /// <summary>
  51. /// SQUAD Spherical Quadrangle interpolation [Shoe87]
  52. /// </summary>
  53. public static Quaternion GetQuatSquad(float t, Quaternion q0, Quaternion q1, Quaternion a0, Quaternion a1)
  54. {
  55. float slerpT = 2.0f * t * (1.0f - t);
  56. Quaternion slerpP = Slerp(q0, q1, t);
  57. Quaternion slerpQ = Slerp(a0, a1, t);
  58. return Slerp(slerpP, slerpQ, slerpT);
  59. }
  60. public static Quaternion GetSquadIntermediate(Quaternion q0, Quaternion q1, Quaternion q2)
  61. {
  62. Quaternion q1Inv = GetQuatConjugate(q1);
  63. Quaternion p0 = GetQuatLog(q1Inv * q0);
  64. Quaternion p2 = GetQuatLog(q1Inv * q2);
  65. Quaternion sum = new Quaternion(-0.25f * (p0.x + p2.x), -0.25f * (p0.y + p2.y), -0.25f * (p0.z + p2.z), -0.25f * (p0.w + p2.w));
  66. return q1 * GetQuatExp(sum);
  67. }
  68. /// <summary>
  69. /// Smooths the input parameter t.
  70. /// If less than k1 ir greater than k2, it uses a sin.
  71. /// Between k1 and k2 it uses linear interp.
  72. /// </summary>
  73. public static float Ease(float t, float k1, float k2)
  74. {
  75. float f; float s;
  76. f = k1 * 2 / Mathf.PI + k2 - k1 + (1.0f - k2) * 2 / Mathf.PI;
  77. if (t < k1)
  78. {
  79. s = k1 * (2 / Mathf.PI) * (Mathf.Sin((t / k1) * Mathf.PI / 2 - Mathf.PI / 2) + 1);
  80. }
  81. else
  82. if (t < k2)
  83. {
  84. s = (2 * k1 / Mathf.PI + t - k1);
  85. }
  86. else
  87. {
  88. s = 2 * k1 / Mathf.PI + k2 - k1 + ((1 - k2) * (2 / Mathf.PI)) * Mathf.Sin(((t - k2) / (1.0f - k2)) * Mathf.PI / 2);
  89. }
  90. return (s / f);
  91. }
  92. /// <summary>
  93. /// We need this because Quaternion.Slerp always uses the shortest arc.
  94. /// </summary>
  95. public static Quaternion Slerp(Quaternion p, Quaternion q, float t)
  96. {
  97. Quaternion ret;
  98. float fCos = Quaternion.Dot(p, q);
  99. if ((1.0f + fCos) > 0.00001)
  100. {
  101. float fCoeff0, fCoeff1;
  102. if ((1.0f - fCos) > 0.00001)
  103. {
  104. float omega = Mathf.Acos(fCos);
  105. float invSin = 1.0f / Mathf.Sin(omega);
  106. fCoeff0 = Mathf.Sin((1.0f - t) * omega) * invSin;
  107. fCoeff1 = Mathf.Sin(t * omega) * invSin;
  108. }
  109. else
  110. {
  111. fCoeff0 = 1.0f - t;
  112. fCoeff1 = t;
  113. }
  114. ret.x = fCoeff0 * p.x + fCoeff1 * q.x;
  115. ret.y = fCoeff0 * p.y + fCoeff1 * q.y;
  116. ret.z = fCoeff0 * p.z + fCoeff1 * q.z;
  117. ret.w = fCoeff0 * p.w + fCoeff1 * q.w;
  118. }
  119. else
  120. {
  121. float fCoeff0 = Mathf.Sin((1.0f - t) * Mathf.PI * 0.5f);
  122. float fCoeff1 = Mathf.Sin(t * Mathf.PI * 0.5f);
  123. ret.x = fCoeff0 * p.x - fCoeff1 * p.y;
  124. ret.y = fCoeff0 * p.y + fCoeff1 * p.x;
  125. ret.z = fCoeff0 * p.z - fCoeff1 * p.w;
  126. ret.w = p.z;
  127. }
  128. return ret;
  129. }
  130. }