HingeJoint.cs 5.4 KB

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