HingeJoint.cs 5.0 KB

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