HingeJoint.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /// Hinge joint removes all but a single rotation degree of freedom from its two attached bodies (for example a door
  11. /// hinge).
  12. /// </summary>
  13. public sealed class HingeJoint : Joint
  14. {
  15. /// <summary>
  16. /// Flags to control hinge joint behaviour.
  17. /// </summary>
  18. [Flags]
  19. internal enum Flag // Note: Must match C++ enum HingeJoint::Flag
  20. {
  21. Limit = 0x01,
  22. Drive = 0x02,
  23. }
  24. [SerializeField]
  25. private SerializableData data = new SerializableData();
  26. /// <summary>
  27. /// Returns the current angle between the two attached bodes.
  28. /// </summary>
  29. public Radian Angle
  30. {
  31. get
  32. {
  33. if (Native != null)
  34. return Native.Angle;
  35. return new Radian(0.0f);
  36. }
  37. }
  38. /// <summary>
  39. /// Returns the current angular speed of the joint.
  40. /// </summary>
  41. public float Speed
  42. {
  43. get
  44. {
  45. if (Native != null)
  46. return Native.Speed;
  47. return 0.0f;
  48. }
  49. }
  50. /// <summary>
  51. /// Determines the limit of the joint. Limit constrains the motion to the specified angle range. You must enable
  52. /// <see cref="EnableLimit"/> for this to be enforced.
  53. /// </summary>
  54. public LimitAngularRange Limit
  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.Limit = value;
  64. }
  65. }
  66. /// <summary>
  67. /// Determines the drive properties of the joint. It drives the joint's angular velocity towards a particular value.
  68. /// You must enable <see cref="EnableDrive"/> for this to be applied.
  69. /// </summary>
  70. public HingeJointDrive Drive
  71. {
  72. get { return [email protected]; }
  73. set
  74. {
  75. if ([email protected] == value)
  76. return;
  77. [email protected] = value;
  78. if (Native != null)
  79. Native.Drive = value;
  80. }
  81. }
  82. /// <summary>
  83. /// Enables or disables a limit that contrains the joint's motion to a specified angle range.
  84. /// </summary>
  85. public bool EnableLimit
  86. {
  87. get { return ([email protected] & Flag.Limit) != 0; }
  88. set
  89. {
  90. if (!SetFlag(Flag.Limit, value))
  91. return;
  92. if (Native != null)
  93. Native.EnableLimit = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Enables or disables a drive that drives the joint's angular velocity towards a particular value.
  98. /// </summary>
  99. public bool EnableDrive
  100. {
  101. get { return ([email protected] & Flag.Drive) != 0; }
  102. set
  103. {
  104. if (!SetFlag(Flag.Drive, value))
  105. return;
  106. if (Native != null)
  107. Native.EnableDrive = value;
  108. }
  109. }
  110. /// <summary>
  111. /// Returns the native joint wrapped by this component.
  112. /// </summary>
  113. private NativeHingeJoint Native
  114. {
  115. get { return (NativeHingeJoint)native; }
  116. }
  117. /// <summary>
  118. /// Toggles a specific distance joint flag on or off.
  119. /// </summary>
  120. /// <param name="flag">Flag to toggle.</param>
  121. /// <param name="enabled">Should the flag be turned on or off.</param>
  122. /// <returns>True if the new newly set flag state was different from the previous one.</returns>
  123. private bool SetFlag(Flag flag, bool enabled)
  124. {
  125. Flag newFlags = [email protected];
  126. if (enabled)
  127. newFlags |= flag;
  128. else
  129. newFlags &= ~flag;
  130. if (newFlags == [email protected])
  131. return false;
  132. [email protected] = newFlags;
  133. return true;
  134. }
  135. /// <inheritdoc/>
  136. internal override NativeJoint CreateNative()
  137. {
  138. NativeHingeJoint joint = new NativeHingeJoint(commonData.@internal, data.@internal);
  139. return joint;
  140. }
  141. /// <summary>
  142. /// Holds all data the joint component needs to persist through serialization.
  143. /// </summary>
  144. [SerializeObject]
  145. internal new class SerializableData
  146. {
  147. public ScriptHingeJointData @internal;
  148. public SerializableData()
  149. {
  150. @internal.limit = new LimitAngularRange();
  151. @internal.drive = new HingeJointDrive();
  152. @internal.flags = 0;
  153. }
  154. }
  155. }
  156. /** @} */
  157. }