DistanceJoint.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Physics
  7. * @{
  8. */
  9. /// <summary>
  10. /// A joint that maintains an upper or lower (or both) bound on the distance between two bodies.
  11. /// </summary>
  12. public sealed class DistanceJoint : Joint
  13. {
  14. /// <summary>
  15. /// Flags to control distance joint behaviour.
  16. /// </summary>
  17. [Flags]
  18. internal enum Flag // Note: Must match C++ enum DistanceJoint::Flag
  19. {
  20. MinDistanceLimit = 0x01,
  21. MaxDistanceLimit = 0x02,
  22. Spring = 0x04
  23. }
  24. [SerializeField]
  25. private SerializableData data = new SerializableData();
  26. /// <summary>
  27. /// Returns the current distance between the two joint bodies.
  28. /// </summary>
  29. public float Distance
  30. {
  31. get
  32. {
  33. if(Native != null)
  34. return Native.Distance;
  35. return 0.0f;
  36. }
  37. }
  38. /// <summary>
  39. /// Determines the minimum distance the bodies are allowed to be at, they will get no closer. You must enable
  40. /// <see cref="EnableMinDistanceLimit"/> in order for this to be enforced.
  41. /// </summary>
  42. public float MinDistance
  43. {
  44. get { return [email protected]; }
  45. set
  46. {
  47. if ([email protected] == value)
  48. return;
  49. [email protected] = value;
  50. if (Native != null)
  51. Native.MinDistance = value;
  52. }
  53. }
  54. /// <summary>
  55. /// Determines the maximum distance the bodies are allowed to be at, they will get no further. You must enable
  56. /// <see cref="EnableMaxDistanceLimit"/> in order for this to be enforced.
  57. /// </summary>
  58. public float MaxDistance
  59. {
  60. get { return [email protected]; }
  61. set
  62. {
  63. if ([email protected] == value)
  64. return;
  65. [email protected] = value;
  66. if (Native != null)
  67. Native.MaxDistance = value;
  68. }
  69. }
  70. /// <summary>
  71. /// Determines the error tolerance of the joint at which the joint becomes active. This value slightly extends the
  72. /// lower and upper limit for precision reasons.
  73. /// </summary>
  74. public float Tolerance
  75. {
  76. get { return [email protected]; }
  77. set
  78. {
  79. if ([email protected] == value)
  80. return;
  81. [email protected] = value;
  82. if (Native != null)
  83. Native.Tolerance = value;
  84. }
  85. }
  86. /// <summary>
  87. /// Returns a spring that controls how the joint responds when a limit is reached. You must enable
  88. /// <see cref="EnableSpring"/> in order for the spring to be applied.
  89. /// </summary>
  90. public Spring Spring
  91. {
  92. get { return [email protected]; }
  93. set
  94. {
  95. if ([email protected] == value)
  96. return;
  97. [email protected] = value;
  98. if (Native != null)
  99. Native.Spring = value;
  100. }
  101. }
  102. /// <summary>
  103. /// Enables or disables the limit that causes joint objects to maintain a minimum distance between themselves.
  104. /// </summary>
  105. public bool EnableMinDistanceLimit
  106. {
  107. get { return ([email protected] & Flag.MinDistanceLimit) != 0; }
  108. set
  109. {
  110. if (!SetFlag(Flag.MinDistanceLimit, value))
  111. return;
  112. if (Native != null)
  113. Native.EnableMinDistanceLimit = value;
  114. }
  115. }
  116. /// <summary>
  117. /// Enables or disables the limit that causes joint objects to maintain a maximum distance between themselves.
  118. /// </summary>
  119. public bool EnableMaxDistanceLimit
  120. {
  121. get { return ([email protected] & Flag.MaxDistanceLimit) != 0; }
  122. set
  123. {
  124. if (!SetFlag(Flag.MaxDistanceLimit, value))
  125. return;
  126. if (Native != null)
  127. Native.EnableMinDistanceLimit = value;
  128. }
  129. }
  130. /// <summary>
  131. /// Enables or disables the spring that controls how the joint reacts when the limit is reached.
  132. /// </summary>
  133. public bool EnableSpring
  134. {
  135. get { return ([email protected] & Flag.Spring) != 0; }
  136. set
  137. {
  138. if (!SetFlag(Flag.Spring, value))
  139. return;
  140. if (Native != null)
  141. Native.EnableSpring = value;
  142. }
  143. }
  144. /// <summary>
  145. /// Toggles a specific distance joint flag on or off.
  146. /// </summary>
  147. /// <param name="flag">Flag to toggle.</param>
  148. /// <param name="enabled">Should the flag be turned on or off.</param>
  149. /// <returns>True if the new newly set flag state was different from the previous one.</returns>
  150. private bool SetFlag(Flag flag, bool enabled)
  151. {
  152. Flag newFlags = [email protected];
  153. if (enabled)
  154. newFlags |= flag;
  155. else
  156. newFlags &= ~flag;
  157. if (newFlags == [email protected])
  158. return false;
  159. [email protected] = newFlags;
  160. return true;
  161. }
  162. /// <summary>
  163. /// Returns the native joint wrapped by this component.
  164. /// </summary>
  165. private NativeDistanceJoint Native
  166. {
  167. get { return (NativeDistanceJoint)native; }
  168. }
  169. /// <inheritdoc/>
  170. internal override NativeJoint CreateNative()
  171. {
  172. NativeDistanceJoint joint = new NativeDistanceJoint(commonData.@internal, data.@internal);
  173. return joint;
  174. }
  175. /// <summary>
  176. /// Holds all data the joint component needs to persist through serialization.
  177. /// </summary>
  178. [SerializeObject]
  179. internal new class SerializableData
  180. {
  181. public ScriptDistanceJointData @internal;
  182. public SerializableData()
  183. {
  184. @internal.minDistance = 0.0f;
  185. @internal.maxDistance = 0.0f;
  186. @internal.tolerance = 0.25f;
  187. @internal.flags = 0;
  188. }
  189. }
  190. }
  191. /** @} */
  192. }