2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.4 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using System;
  3. using UnityEngine;
  4. namespace HTC.UnityPlugin.Utility
  5. {
  6. [Serializable]
  7. public struct RigidPose
  8. {
  9. public Vector3 pos;
  10. public Quaternion rot;
  11. public static RigidPose identity
  12. {
  13. get { return new RigidPose(Vector3.zero, Quaternion.identity); }
  14. }
  15. public RigidPose(Vector3 pos, Quaternion rot)
  16. {
  17. this.pos = pos;
  18. this.rot = rot;
  19. }
  20. public RigidPose(Transform t, bool useLocal = false)
  21. {
  22. if (t == null)
  23. {
  24. pos = Vector3.zero;
  25. rot = Quaternion.identity;
  26. }
  27. else if (!useLocal)
  28. {
  29. pos = t.position;
  30. rot = t.rotation;
  31. }
  32. else
  33. {
  34. pos = t.localPosition;
  35. rot = t.localRotation;
  36. }
  37. }
  38. public Vector3 forward { get { return rot * Vector3.forward; } }
  39. public Vector3 right { get { return rot * Vector3.right; } }
  40. public Vector3 up { get { return rot * Vector3.up; } }
  41. public override bool Equals(object o)
  42. {
  43. if (o is RigidPose)
  44. {
  45. var t = (RigidPose)o;
  46. return pos == t.pos && rot == t.rot;
  47. }
  48. return false;
  49. }
  50. public override int GetHashCode()
  51. {
  52. return pos.GetHashCode() ^ rot.GetHashCode();
  53. }
  54. public static bool operator ==(RigidPose a, RigidPose b)
  55. {
  56. return
  57. a.pos.x == b.pos.x &&
  58. a.pos.y == b.pos.y &&
  59. a.pos.z == b.pos.z &&
  60. a.rot.x == b.rot.x &&
  61. a.rot.y == b.rot.y &&
  62. a.rot.z == b.rot.z &&
  63. a.rot.w == b.rot.w;
  64. }
  65. public static bool operator !=(RigidPose a, RigidPose b)
  66. {
  67. return !(a == b);
  68. }
  69. public static RigidPose operator *(RigidPose a, RigidPose b)
  70. {
  71. return new RigidPose
  72. {
  73. rot = a.rot * b.rot,
  74. pos = a.pos + a.rot * b.pos
  75. };
  76. }
  77. public void Multiply(RigidPose a, RigidPose b)
  78. {
  79. rot = a.rot * b.rot;
  80. pos = a.pos + a.rot * b.pos;
  81. }
  82. public void Inverse()
  83. {
  84. rot = Quaternion.Inverse(rot);
  85. pos = -(rot * pos);
  86. }
  87. public RigidPose GetInverse()
  88. {
  89. var t = new RigidPose(pos, rot);
  90. t.Inverse();
  91. return t;
  92. }
  93. public Vector3 InverseTransformPoint(Vector3 point)
  94. {
  95. return Quaternion.Inverse(rot) * (point - pos);
  96. }
  97. public Vector3 TransformPoint(Vector3 point)
  98. {
  99. return pos + (rot * point);
  100. }
  101. public static RigidPose Lerp(RigidPose a, RigidPose b, float t)
  102. {
  103. return new RigidPose(Vector3.Lerp(a.pos, b.pos, t), Quaternion.Slerp(a.rot, b.rot, t));
  104. }
  105. public void Lerp(RigidPose to, float t)
  106. {
  107. pos = Vector3.Lerp(pos, to.pos, t);
  108. rot = Quaternion.Slerp(rot, to.rot, t);
  109. }
  110. public static RigidPose LerpUnclamped(RigidPose a, RigidPose b, float t)
  111. {
  112. return new RigidPose(Vector3.LerpUnclamped(a.pos, b.pos, t), Quaternion.SlerpUnclamped(a.rot, b.rot, t));
  113. }
  114. public void LerpUnclamped(RigidPose to, float t)
  115. {
  116. pos = Vector3.LerpUnclamped(pos, to.pos, t);
  117. rot = Quaternion.SlerpUnclamped(rot, to.rot, t);
  118. }
  119. public static void SetPose(Transform target, RigidPose pose, Transform origin = null)
  120. {
  121. if (origin != null && origin != target.parent)
  122. {
  123. target.position = origin.transform.TransformPoint(pose.pos);
  124. target.rotation = origin.rotation * pose.rot;
  125. }
  126. else
  127. {
  128. target.localPosition = pose.pos;
  129. target.localRotation = pose.rot;
  130. }
  131. }
  132. // proper following duration is larger then 0.02 second, depends on the update rate
  133. public static void SetRigidbodyVelocity(Rigidbody rigidbody, Vector3 from, Vector3 to, float duration)
  134. {
  135. var diffPos = to - from;
  136. if (Mathf.Approximately(diffPos.sqrMagnitude, 0f))
  137. {
  138. rigidbody.velocity = Vector3.zero;
  139. }
  140. else
  141. {
  142. rigidbody.velocity = diffPos / duration;
  143. }
  144. }
  145. // proper folloing duration is larger then 0.02 second, depends on the update rate
  146. public static void SetRigidbodyAngularVelocity(Rigidbody rigidbody, Quaternion from, Quaternion to, float duration, bool overrideMaxAngularVelocity = true)
  147. {
  148. float angle;
  149. Vector3 axis;
  150. var fromToRot = to * Quaternion.Inverse(from);
  151. fromToRot.ToAngleAxis(out angle, out axis);
  152. while (angle > 180f) { angle -= 360f; }
  153. if (Mathf.Approximately(angle, 0f) || float.IsNaN(axis.x) || float.IsNaN(axis.y) || float.IsNaN(axis.z))
  154. {
  155. rigidbody.angularVelocity = Vector3.zero;
  156. }
  157. else
  158. {
  159. angle *= Mathf.Deg2Rad / duration; // convert to radius speed
  160. if (overrideMaxAngularVelocity && rigidbody.maxAngularVelocity < angle) { rigidbody.maxAngularVelocity = angle; }
  161. rigidbody.angularVelocity = axis * angle;
  162. }
  163. }
  164. public static RigidPose FromToPose(RigidPose from, RigidPose to)
  165. {
  166. var invRot = Quaternion.Inverse(from.rot);
  167. return new RigidPose(invRot * (to.pos - from.pos), invRot * to.rot);
  168. }
  169. #if UNITY_2017_2_OR_NEWER
  170. public static implicit operator RigidPose(Pose v)
  171. {
  172. return new RigidPose(v.position, v.rotation);
  173. }
  174. public static implicit operator Pose(RigidPose v)
  175. {
  176. return new Pose(v.pos, v.rot);
  177. }
  178. #endif
  179. public override string ToString()
  180. {
  181. return "{p" + pos.ToString() + ",r" + rot.ToString() + "}";
  182. }
  183. public string ToString(string format)
  184. {
  185. return "{p" + pos.ToString(format) + ",r" + rot.ToString(format) + "}";
  186. }
  187. }
  188. }