JointGizmos.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup Gizmos
  8. * @{
  9. */
  10. /// <summary>
  11. /// Handles drawing of gizmos for all the types of <see cref="Joint"/> component.
  12. /// </summary>
  13. internal class JointGizmos
  14. {
  15. /// <summary>
  16. /// Returns the anchor position for the specified joint body. Anchor represents the world position of the rigidbody
  17. /// added to the offset provided by the joint.
  18. /// </summary>
  19. /// <param name="joint">Joint from which to retrieve the body.</param>
  20. /// <param name="body">Body to retrieve the anchor for.</param>
  21. /// <returns>Anchor position in world space.</returns>
  22. private static Vector3 GetAnchor(Joint joint, JointBody body)
  23. {
  24. Rigidbody rigidbody = joint.GetBody(body);
  25. Vector3 anchor = joint.GetPosition(body);
  26. if (rigidbody != null)
  27. {
  28. Quaternion worldRot = rigidbody.SceneObject.Rotation;
  29. anchor = worldRot.Rotate(anchor) + rigidbody.SceneObject.Position;
  30. }
  31. else
  32. {
  33. Quaternion worldRot = joint.SceneObject.Rotation;
  34. anchor = worldRot.Rotate(anchor) + joint.SceneObject.Position;
  35. }
  36. return anchor;
  37. }
  38. /// <summary>
  39. /// Method called by the runtime when joints are meant to be drawn.
  40. /// </summary>
  41. /// <param name="joint">Joint to draw gizmos for.</param>
  42. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  43. private static void DrawFixedJoint(FixedJoint joint)
  44. {
  45. Vector3 target = GetAnchor(joint, JointBody.Target);
  46. Vector3 anchor = GetAnchor(joint, JointBody.Anchor);
  47. Gizmos.Color = Color.White;
  48. Gizmos.DrawSphere(target, 0.05f);
  49. Gizmos.DrawSphere(anchor, 0.05f);
  50. Gizmos.Color = Color.Green;
  51. Gizmos.DrawLine(target, anchor);
  52. }
  53. /// <summary>
  54. /// Method called by the runtime when joints are meant to be drawn.
  55. /// </summary>
  56. /// <param name="joint">Joint to draw gizmos for.</param>
  57. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  58. private static void DrawDistanceJoint(DistanceJoint joint)
  59. {
  60. Vector3 anchorA = GetAnchor(joint, JointBody.Target);
  61. Vector3 anchorB = GetAnchor(joint, JointBody.Anchor);
  62. Gizmos.Color = Color.White;
  63. Gizmos.DrawSphere(anchorA, 0.05f);
  64. Gizmos.DrawSphere(anchorB, 0.05f);
  65. Gizmos.Color = Color.Red;
  66. Vector3 diff = anchorB - anchorA;
  67. float length = diff.Length;
  68. Vector3 normal = diff.Normalized;
  69. float min = 0.0f;
  70. float max = length;
  71. if (joint.HasFlag(DistanceJointFlag.MinDistance))
  72. {
  73. min = MathEx.Max(0.0f, joint.MinDistance);
  74. if (joint.HasFlag(DistanceJointFlag.MaxDistance))
  75. min = MathEx.Min(min, MathEx.Min(10000.0f, joint.MaxDistance));
  76. Gizmos.DrawLine(anchorA, anchorA + normal * min);
  77. }
  78. if (joint.HasFlag(DistanceJointFlag.MaxDistance))
  79. {
  80. max = MathEx.Min(10000.0f, joint.MaxDistance);
  81. if (joint.HasFlag(DistanceJointFlag.MinDistance))
  82. max = MathEx.Max(max, min);
  83. if (length > max)
  84. Gizmos.DrawLine(anchorA + normal * max, anchorA + normal * length);
  85. }
  86. Gizmos.Color = Color.Green;
  87. Gizmos.DrawLine(anchorA + normal * min, anchorA + normal * MathEx.Min(max, length));
  88. }
  89. /// <summary>
  90. /// Method called by the runtime when joints are meant to be drawn.
  91. /// </summary>
  92. /// <param name="joint">Joint to draw gizmos for.</param>
  93. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  94. private static void DrawSliderJoint(SliderJoint joint)
  95. {
  96. Vector3 anchor = GetAnchor(joint, JointBody.Anchor);
  97. Vector3 target = GetAnchor(joint, JointBody.Target);
  98. Vector3 normal = -joint.SceneObject.Right;
  99. if (joint.HasFlag(SliderJointFlag.Limit))
  100. {
  101. LimitLinearRange limit = joint.Limit;
  102. float max = MathEx.Min(10000.0f, limit.upper);
  103. float min = MathEx.Clamp(limit.lower, 0.0f, max);
  104. max = MathEx.Max(max, min);
  105. Gizmos.Color = Color.Red;
  106. Gizmos.DrawLine(anchor, anchor + normal*min);
  107. Gizmos.Color = Color.Green;
  108. Gizmos.DrawLine(anchor + normal*min, anchor + normal*max);
  109. }
  110. else
  111. {
  112. Gizmos.Color = Color.Green;
  113. float length = 100.0f;
  114. Gizmos.DrawLine(anchor, anchor + normal * length);
  115. }
  116. Gizmos.Color = Color.Yellow;
  117. Gizmos.DrawSphere(target, 0.05f);
  118. }
  119. /// <summary>
  120. /// Method called by the runtime when joints are meant to be drawn.
  121. /// </summary>
  122. /// <param name="joint">Joint to draw gizmos for.</param>
  123. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  124. private static void DrawSphericalJoint(SphericalJoint joint)
  125. {
  126. Vector3 target = GetAnchor(joint, JointBody.Target);
  127. Vector3 anchor = GetAnchor(joint, JointBody.Anchor);
  128. Vector3 center = target;
  129. Rigidbody rigidbody = joint.GetBody(JointBody.Target);
  130. if (rigidbody != null)
  131. center = rigidbody.SceneObject.Position;
  132. Gizmos.Color = Color.White;
  133. Gizmos.DrawSphere(center, 0.05f);
  134. Gizmos.Color = Color.Yellow;
  135. Gizmos.DrawSphere(target, 0.05f);
  136. Gizmos.DrawSphere(anchor, 0.05f);
  137. Gizmos.Color = Color.Green;
  138. Gizmos.DrawLine(target, center);
  139. Gizmos.Color = Color.Green;
  140. if (joint.HasFlag(SphericalJointFlag.Limit))
  141. {
  142. LimitConeRange limit = joint.Limit;
  143. Radian zAngle = MathEx.Min(new Degree(360), limit.zLimitAngle * 2.0f);
  144. Radian yAngle = MathEx.Min(new Degree(360), limit.yLimitAngle * 2.0f);
  145. Gizmos.Transform = joint.SceneObject.WorldTransform;
  146. Gizmos.DrawWireArc(Vector3.Zero, Vector3.ZAxis, 0.25f, zAngle * -0.5f + new Degree(90), zAngle);
  147. Gizmos.DrawWireArc(Vector3.Zero, Vector3.YAxis, 0.25f, yAngle * -0.5f + new Degree(90), yAngle);
  148. Gizmos.Color = Color.Red;
  149. Radian remainingZAngle = new Degree(360) - zAngle;
  150. Radian remainingYAngle = new Degree(360) - yAngle;
  151. Gizmos.DrawWireArc(Vector3.Zero, Vector3.ZAxis, 0.25f, zAngle * 0.5f + new Degree(90), remainingZAngle);
  152. Gizmos.DrawWireArc(Vector3.Zero, Vector3.YAxis, 0.25f, yAngle * 0.5f + new Degree(90), remainingYAngle);
  153. }
  154. else
  155. {
  156. Gizmos.Color = Color.Green;
  157. Gizmos.Transform = joint.SceneObject.WorldTransform;
  158. Gizmos.DrawWireDisc(Vector3.Zero, Vector3.ZAxis, 0.25f);
  159. Gizmos.DrawWireDisc(Vector3.Zero, Vector3.YAxis, 0.25f);
  160. }
  161. }
  162. /// <summary>
  163. /// Method called by the runtime when joints are meant to be drawn.
  164. /// </summary>
  165. /// <param name="joint">Joint to draw gizmos for.</param>
  166. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  167. private static void DrawHingeJoint(HingeJoint joint)
  168. {
  169. Vector3 target = GetAnchor(joint, JointBody.Target);
  170. Vector3 anchor = GetAnchor(joint, JointBody.Anchor);
  171. Vector3 center = target;
  172. Rigidbody rigidbody = joint.GetBody(JointBody.Target);
  173. if (rigidbody != null)
  174. center = rigidbody.SceneObject.Position;
  175. Gizmos.Color = Color.White;
  176. Gizmos.DrawSphere(center, 0.05f);
  177. Gizmos.Color = Color.Yellow;
  178. Gizmos.DrawSphere(target, 0.05f);
  179. Gizmos.DrawSphere(anchor, 0.05f);
  180. Gizmos.Color = Color.Green;
  181. Gizmos.DrawLine(target, center);
  182. const float radius = 0.25f;
  183. const float height = 0.5f;
  184. if (joint.HasFlag(HingeJointFlag.Limit))
  185. {
  186. Gizmos.Transform = joint.SceneObject.WorldTransform;
  187. LimitAngularRange limit = joint.Limit;
  188. Action<float> drawLimitedArc = x =>
  189. {
  190. Degree lower = MathEx.WrapAngle(limit.lower);
  191. Degree upper = MathEx.WrapAngle(limit.upper);
  192. lower = MathEx.Min(lower, upper);
  193. upper = MathEx.Max(upper, lower);
  194. // Arc zero to lower limit
  195. Gizmos.Color = Color.Red;
  196. Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, new Degree(0.0f), lower);
  197. // Arc lower to upper limit
  198. Degree validRange = upper - lower;
  199. Gizmos.Color = Color.Green;
  200. Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, lower, validRange);
  201. // Arc upper to full circle
  202. Degree remainingRange = new Degree(360) - upper;
  203. Gizmos.Color = Color.Red;
  204. Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, upper, remainingRange);
  205. };
  206. drawLimitedArc(-height);
  207. drawLimitedArc(height);
  208. }
  209. else
  210. {
  211. Gizmos.Color = Color.Green;
  212. Gizmos.Transform = joint.SceneObject.WorldTransform;
  213. Gizmos.DrawWireDisc(Vector3.XAxis * -height, Vector3.XAxis, radius);
  214. Gizmos.DrawWireDisc(Vector3.XAxis * height, Vector3.XAxis, radius);
  215. }
  216. Vector3[] lineStartPoints = new Vector3[4];
  217. lineStartPoints[0] = new Vector3(-height, radius, 0);
  218. lineStartPoints[1] = new Vector3(-height, -radius, 0);
  219. lineStartPoints[2] = new Vector3(-height, 0, radius);
  220. lineStartPoints[3] = new Vector3(-height, 0, -radius);
  221. Vector3[] lineEndPoints = new Vector3[4];
  222. lineEndPoints[0] = new Vector3(height, radius, 0);
  223. lineEndPoints[1] = new Vector3(height, -radius, 0);
  224. lineEndPoints[2] = new Vector3(height, 0, radius);
  225. lineEndPoints[3] = new Vector3(height, 0, -radius);
  226. Gizmos.Color = Color.Green;
  227. for (int i = 0; i < 4; i++)
  228. Gizmos.DrawLine(lineStartPoints[i], lineEndPoints[i]);
  229. }
  230. /// <summary>
  231. /// Method called by the runtime when joints are meant to be drawn.
  232. /// </summary>
  233. /// <param name="joint">Joint to draw gizmos for.</param>
  234. [DrawGizmo(DrawGizmoFlags.Selected | DrawGizmoFlags.ParentSelected)]
  235. private static void DrawD6Joint(D6Joint joint)
  236. {
  237. Vector3 anchorA = GetAnchor(joint, JointBody.Target);
  238. Vector3 anchorB = GetAnchor(joint, JointBody.Anchor);
  239. Gizmos.Color = Color.White;
  240. Gizmos.DrawSphere(anchorA, 0.05f);
  241. Gizmos.DrawSphere(anchorB, 0.05f);
  242. }
  243. }
  244. /** @} */
  245. }