D6Joint.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Represents the most customizable type of joint. This joint type can be used to create all other built-in joint
  7. /// types, and to design your own custom ones, but is less intuitive to use.Allows a specification of a linear
  8. /// constraint (for example for slider), twist constraint(rotating around X) and swing constraint(rotating around Y and
  9. /// Z). It also allows you to constrain limits to only specific axes or completely lock specific axes.
  10. /// </summary>
  11. public sealed class D6Joint : Joint
  12. {
  13. [SerializeField]
  14. private SerializableData data = new SerializableData();
  15. /// <summary>
  16. /// Returns the current rotation of the joint around the X axis.
  17. /// </summary>
  18. public Radian Twist
  19. {
  20. get
  21. {
  22. if (Native != null)
  23. return Native.Twist;
  24. return new Radian(0.0f);
  25. }
  26. }
  27. /// <summary>
  28. /// Returns the current rotation of the joint around the Y axis.
  29. /// </summary>
  30. public Radian SwingY
  31. {
  32. get
  33. {
  34. if (Native != null)
  35. return Native.SwingY;
  36. return new Radian(0.0f);
  37. }
  38. }
  39. /// <summary>
  40. /// Returns the current rotation of the joint around the Z axis.
  41. /// </summary>
  42. public Radian SwingZ
  43. {
  44. get
  45. {
  46. if (Native != null)
  47. return Native.SwingZ;
  48. return new Radian(0.0f);
  49. }
  50. }
  51. /// <summary>
  52. /// Linear limit used for constraining translation degrees of freedom.
  53. /// </summary>
  54. public LimitLinear LimitLinear
  55. {
  56. get { return [email protected]; }
  57. set
  58. {
  59. if ([email protected] == value)
  60. return;
  61. [email protected] = value;
  62. if (Native != null)
  63. Native.LimitLinear = value;
  64. }
  65. }
  66. /// <summary>
  67. /// Angular limit used for constraining the twist (rotation around X) degree of freedom.
  68. /// </summary>
  69. public LimitAngularRange LimitTwist
  70. {
  71. get { return [email protected]; }
  72. set
  73. {
  74. if ([email protected] == value)
  75. return;
  76. [email protected] = value;
  77. if (Native != null)
  78. Native.LimitTwist = value;
  79. }
  80. }
  81. /// <summary>
  82. /// Cone limit used for constraining the swing (rotation around Y and Z) degree of freedom.
  83. /// </summary>
  84. public LimitConeRange LimitSwing
  85. {
  86. get { return [email protected]; }
  87. set
  88. {
  89. if ([email protected] == value)
  90. return;
  91. [email protected] = value;
  92. if (Native != null)
  93. Native.LimitSwing = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Determines the drive's target position relative to the joint's first body. This is the position the drive will
  98. /// attempt to reach if enabled.
  99. /// </summary>
  100. public Vector3 DrivePosition
  101. {
  102. get { return [email protected]; }
  103. set
  104. {
  105. if ([email protected] == value)
  106. return;
  107. [email protected] = value;
  108. if (Native != null)
  109. Native.DrivePosition = value;
  110. }
  111. }
  112. /// <summary>
  113. /// Determines the drive's target orientation relative to the joint's first body. This is the orientation the drive
  114. /// will attempt to reach if enabled.
  115. /// </summary>
  116. public Quaternion DriveRotation
  117. {
  118. get { return [email protected]; }
  119. set
  120. {
  121. if ([email protected] == value)
  122. return;
  123. [email protected] = value;
  124. if (Native != null)
  125. Native.DriveRotation = value;
  126. }
  127. }
  128. /// <summary>
  129. /// Determines the drive's target linear velocity. This is the velocity the drive will attempt to reach if enabled.
  130. /// </summary>
  131. public Vector3 DriveLinearVelocity
  132. {
  133. get { return [email protected]; }
  134. set
  135. {
  136. if ([email protected] == value)
  137. return;
  138. [email protected] = value;
  139. if (Native != null)
  140. Native.DriveLinearVelocity = value;
  141. }
  142. }
  143. /// <summary>
  144. /// Determines the drive's target angular velocity. This is the velocity the drive will attempt to reach if enabled.
  145. /// </summary>
  146. public Vector3 DriveAngularVelocity
  147. {
  148. get { return [email protected]; }
  149. set
  150. {
  151. if ([email protected] == value)
  152. return;
  153. [email protected] = value;
  154. if (Native != null)
  155. Native.DriveAngularVelocity = value;
  156. }
  157. }
  158. /// <summary>
  159. /// Returns the type of motion constrain for the specified axis.
  160. /// </summary>
  161. /// <param name="axis">Axis to retrieve the motion constrain for.</param>
  162. /// <returns>Motion constrain type for the axis.</returns>
  163. public D6JointMotion GetMotion(D6JointAxis axis)
  164. {
  165. return [email protected][(int) axis];
  166. }
  167. /// <summary>
  168. /// Allows you to constrain motion of the specified axis. Be aware that when setting drives for a specific axis
  169. /// you must also take care not to constrain its motion in a conflicting way(for example you cannot add a drive
  170. /// that moves the joint on X axis, and then lock the X axis).
  171. ///
  172. /// Unlocking translations degrees of freedom allows the bodies to move along the subset of the unlocked axes.
  173. /// (for example unlocking just one translational axis is the equivalent of a slider joint.)
  174. ///
  175. /// Angular degrees of freedom are partitioned as twist(around X axis) and swing(around Y and Z axes). Different
  176. /// effects can be achieves by unlocking their various combinations:
  177. /// - If a single degree of angular freedom is unlocked it should be the twist degree as it has extra options for
  178. /// that case (for example for a hinge joint).
  179. /// - If both swing degrees are unlocked but twist is locked the result is a zero-twist joint.
  180. /// - If one swing and one twist degree of freedom are unlocked the result is a zero-swing joint (for example an
  181. /// arm attached at the elbow)
  182. /// - If all angular degrees of freedom are unlocked the result is the same as the spherical joint.
  183. /// </summary>
  184. /// <param name="axis">Axis to change the motion type for.</param>
  185. /// <param name="motion">Type of motion for the axis.</param>
  186. public void SetMotion(D6JointAxis axis, D6JointMotion motion)
  187. {
  188. if ([email protected][(int)axis] == motion)
  189. return;
  190. [email protected][(int)axis] = motion;
  191. if (Native != null)
  192. Native.SetMotion(axis, motion);
  193. }
  194. /// <summary>
  195. /// Returns properties for the specified drive type.
  196. /// </summary>
  197. /// <param name="type">Type of drive to retrieve properties for.</param>
  198. /// <returns>Properties for the requested drive type.</returns>
  199. public D6JointDrive GetDrive(D6JointDriveType type)
  200. {
  201. return [email protected][(int) type];
  202. }
  203. /// <summary>
  204. /// Sets a drive that will attempt to move the specified degree(s) of freedom to the wanted position and velocity.
  205. /// </summary>
  206. /// <param name="type">Type of the drive.</param>
  207. /// <param name="drive">Drive properties.</param>
  208. public void SetDrive(D6JointDriveType type, D6JointDrive drive)
  209. {
  210. if ([email protected][(int)type] == drive)
  211. return;
  212. [email protected][(int)type] = drive;
  213. if (Native != null)
  214. Native.SetDrive(type, drive);
  215. }
  216. /// <summary>
  217. /// Returns the native joint wrapped by this component.
  218. /// </summary>
  219. private NativeD6Joint Native
  220. {
  221. get { return (NativeD6Joint)native; }
  222. }
  223. /// <inheritdoc/>
  224. internal override NativeJoint CreateNative()
  225. {
  226. NativeD6Joint joint = new NativeD6Joint(commonData.@internal, data.@internal);
  227. return joint;
  228. }
  229. /// <summary>
  230. /// Holds all data the joint component needs to persist through serialization.
  231. /// </summary>
  232. [SerializeObject]
  233. internal new class SerializableData
  234. {
  235. public ScriptD6JointData @internal;
  236. public SerializableData()
  237. {
  238. @internal.linearLimit = new LimitLinear();
  239. @internal.twistLimit = new LimitAngularRange();
  240. @internal.swingLimit = new LimitConeRange();
  241. @internal.motion = new D6JointMotion[(int)D6JointAxis.Count];
  242. @internal.drives = new D6JointDrive[(int)D6JointDriveType.Count];
  243. @internal.drivePosition = Vector3.Zero;
  244. @internal.driveRotation = Quaternion.Identity;
  245. @internal.driveLinearVelocity = Vector3.Zero;
  246. @internal.driveAngularVelocity = Vector3.Zero;
  247. }
  248. }
  249. }
  250. }