DistanceJoint.cs 7.0 KB

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