当前位置:   article > 正文

【Unity】Mathd系列(四)Double型Quaternion_unity double类型怎么简写1.9999999999999999e+39

unity double类型怎么简写1.9999999999999999e+39

    其实,单纯要说四元数float型的精度完全够用,并不需要写成double型。不过前面的都写了,总要进行计算啊!不写不行啊。

    Quaternion写起来有些难度,查找了一些资料后仍有一个FromToRotation方法未实现,求大神前来帮助~

Quaterniond:

  1. /// Quaterniond.cs
  2. ///
  3. /// The double type version of the Unity struct Quaternion.
  4. /// It can solve the problem that the float type may not be accurate enough.
  5. /// Function LookAt is unimplmented.
  6. ///
  7. /// Unity Quaternion结构体的double版实现,以解决float型精度可能不够的问题。其中FromToRotation方法暂未实现。
  8. ///
  9. /// Created by D子宇 on 2018.3.17
  10. ///
  11. /// Email: darkziyu@126.com
  12. using System;
  13. using UnityEngine.Internal;
  14. namespace Mathd
  15. {
  16. public struct Quaterniond
  17. {
  18. #region public members
  19. public double x;
  20. public double y;
  21. public double z;
  22. public double w;
  23. #endregion
  24. #region constructor
  25. public Quaterniond(double p_x, double p_y, double p_z, double p_w)
  26. {
  27. x = p_x;
  28. y = p_y;
  29. z = p_z;
  30. w = p_w;
  31. }
  32. #endregion
  33. #region public properties
  34. public double this[int index]
  35. {
  36. get
  37. {
  38. switch (index)
  39. {
  40. case 0:
  41. return x;
  42. case 1:
  43. return y;
  44. case 2:
  45. return z;
  46. case 3:
  47. return w;
  48. default:
  49. throw new IndexOutOfRangeException("Invalid Quaterniond index!");
  50. }
  51. }
  52. set
  53. {
  54. switch (index)
  55. {
  56. case 0:
  57. x = value;
  58. break;
  59. case 1:
  60. y = value;
  61. break;
  62. case 2:
  63. z = value;
  64. break;
  65. case 3:
  66. w = value;
  67. break;
  68. default:
  69. throw new IndexOutOfRangeException("Invalid Quaterniond index!");
  70. }
  71. }
  72. }
  73. public static Quaterniond identity
  74. {
  75. get
  76. {
  77. return new Quaterniond(0, 0, 0, 1);
  78. }
  79. }
  80. public Vector3d eulerAngles
  81. {
  82. get
  83. {
  84. Matrix4x4d m = QuaternionToMatrix(this);
  85. return (MatrixToEuler(m) * 180 / Math.PI);
  86. }
  87. set
  88. {
  89. this = Euler(value);
  90. }
  91. }
  92. #endregion
  93. #region public functions
  94. /// <summary>
  95. /// 夹角大小
  96. /// </summary>
  97. /// <param name="a"></param>
  98. /// <param name="b"></param>
  99. /// <returns></returns>
  100. public static double Angle(Quaterniond a, Quaterniond b)
  101. {
  102. double single = Dot(a, b);
  103. return Math.Acos(Math.Min(Math.Abs(single), 1f)) * 2f * (180 / Math.PI);
  104. }
  105. /// <summary>
  106. /// 轴向旋转
  107. /// </summary>
  108. /// <param name="angle"></param>
  109. /// <param name="axis"></param>
  110. /// <returns></returns>
  111. public static Quaterniond AngleAxis(double angle, Vector3d axis)
  112. {
  113. axis = axis.normalized;
  114. angle = angle / 180D * Math.PI;
  115. Quaterniond q = new Quaterniond();
  116. double halfAngle = angle * 0.5D;
  117. double s = Math.Sin(halfAngle);
  118. q.w = Math.Cos(halfAngle);
  119. q.x = s * axis.x;
  120. q.y = s * axis.y;
  121. q.z = s * axis.z;
  122. return q;
  123. }
  124. /// <summary>
  125. /// 点乘
  126. /// </summary>
  127. /// <param name="a"></param>
  128. /// <param name="b"></param>
  129. /// <returns></returns>
  130. public static double Dot(Quaterniond a, Quaterniond b)
  131. {
  132. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  133. }
  134. /// <summary>
  135. /// 欧拉角转四元数
  136. /// </
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/108436
推荐阅读
相关标签
  

闽ICP备14008679号