D6Joint.cs 11 KB

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