b2_revolute_joint.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_REVOLUTE_JOINT_H
  19. #define B2_REVOLUTE_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Revolute joint definition. This requires defining an anchor point where the
  23. /// bodies are joined. The definition uses local anchor points so that the
  24. /// initial configuration can violate the constraint slightly. You also need to
  25. /// specify the initial relative angle for joint limits. This helps when saving
  26. /// and loading a game.
  27. /// The local anchor points are measured from the body's origin
  28. /// rather than the center of mass because:
  29. /// 1. you might not know where the center of mass will be.
  30. /// 2. if you add/remove shapes from a body and recompute the mass,
  31. /// the joints will be broken.
  32. struct B2_API b2RevoluteJointDef : public b2JointDef
  33. {
  34. b2RevoluteJointDef()
  35. {
  36. type = e_revoluteJoint;
  37. localAnchorA.Set(0.0f, 0.0f);
  38. localAnchorB.Set(0.0f, 0.0f);
  39. referenceAngle = 0.0f;
  40. lowerAngle = 0.0f;
  41. upperAngle = 0.0f;
  42. maxMotorTorque = 0.0f;
  43. motorSpeed = 0.0f;
  44. enableLimit = false;
  45. enableMotor = false;
  46. }
  47. /// Initialize the bodies, anchors, and reference angle using a world
  48. /// anchor point.
  49. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
  50. /// The local anchor point relative to bodyA's origin.
  51. b2Vec2 localAnchorA;
  52. /// The local anchor point relative to bodyB's origin.
  53. b2Vec2 localAnchorB;
  54. /// The bodyB angle minus bodyA angle in the reference state (radians).
  55. float referenceAngle;
  56. /// A flag to enable joint limits.
  57. bool enableLimit;
  58. /// The lower angle for the joint limit (radians).
  59. float lowerAngle;
  60. /// The upper angle for the joint limit (radians).
  61. float upperAngle;
  62. /// A flag to enable the joint motor.
  63. bool enableMotor;
  64. /// The desired motor speed. Usually in radians per second.
  65. float motorSpeed;
  66. /// The maximum motor torque used to achieve the desired motor speed.
  67. /// Usually in N-m.
  68. float maxMotorTorque;
  69. };
  70. /// A revolute joint constrains two bodies to share a common point while they
  71. /// are free to rotate about the point. The relative rotation about the shared
  72. /// point is the joint angle. You can limit the relative rotation with
  73. /// a joint limit that specifies a lower and upper angle. You can use a motor
  74. /// to drive the relative rotation about the shared point. A maximum motor torque
  75. /// is provided so that infinite forces are not generated.
  76. class B2_API b2RevoluteJoint : public b2Joint
  77. {
  78. public:
  79. b2Vec2 GetAnchorA() const override;
  80. b2Vec2 GetAnchorB() const override;
  81. /// The local anchor point relative to bodyA's origin.
  82. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  83. /// The local anchor point relative to bodyB's origin.
  84. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  85. /// Get the reference angle.
  86. float GetReferenceAngle() const { return m_referenceAngle; }
  87. /// Get the current joint angle in radians.
  88. float GetJointAngle() const;
  89. /// Get the current joint angle speed in radians per second.
  90. float GetJointSpeed() const;
  91. /// Is the joint limit enabled?
  92. bool IsLimitEnabled() const;
  93. /// Enable/disable the joint limit.
  94. void EnableLimit(bool flag);
  95. /// Get the lower joint limit in radians.
  96. float GetLowerLimit() const;
  97. /// Get the upper joint limit in radians.
  98. float GetUpperLimit() const;
  99. /// Set the joint limits in radians.
  100. void SetLimits(float lower, float upper);
  101. /// Is the joint motor enabled?
  102. bool IsMotorEnabled() const;
  103. /// Enable/disable the joint motor.
  104. void EnableMotor(bool flag);
  105. /// Set the motor speed in radians per second.
  106. void SetMotorSpeed(float speed);
  107. /// Get the motor speed in radians per second.
  108. float GetMotorSpeed() const;
  109. /// Set the maximum motor torque, usually in N-m.
  110. void SetMaxMotorTorque(float torque);
  111. float GetMaxMotorTorque() const { return m_maxMotorTorque; }
  112. /// Get the reaction force given the inverse time step.
  113. /// Unit is N.
  114. b2Vec2 GetReactionForce(float inv_dt) const override;
  115. /// Get the reaction torque due to the joint limit given the inverse time step.
  116. /// Unit is N*m.
  117. float GetReactionTorque(float inv_dt) const override;
  118. /// Get the current motor torque given the inverse time step.
  119. /// Unit is N*m.
  120. float GetMotorTorque(float inv_dt) const;
  121. /// Dump to b2Log.
  122. void Dump() override;
  123. ///
  124. void Draw(b2Draw* draw) const override;
  125. protected:
  126. friend class b2Joint;
  127. friend class b2GearJoint;
  128. b2RevoluteJoint(const b2RevoluteJointDef* def);
  129. void InitVelocityConstraints(const b2SolverData& data) override;
  130. void SolveVelocityConstraints(const b2SolverData& data) override;
  131. bool SolvePositionConstraints(const b2SolverData& data) override;
  132. // Solver shared
  133. b2Vec2 m_localAnchorA;
  134. b2Vec2 m_localAnchorB;
  135. b2Vec2 m_impulse;
  136. float m_motorImpulse;
  137. float m_lowerImpulse;
  138. float m_upperImpulse;
  139. bool m_enableMotor;
  140. float m_maxMotorTorque;
  141. float m_motorSpeed;
  142. bool m_enableLimit;
  143. float m_referenceAngle;
  144. float m_lowerAngle;
  145. float m_upperAngle;
  146. // Solver temp
  147. int32 m_indexA;
  148. int32 m_indexB;
  149. b2Vec2 m_rA;
  150. b2Vec2 m_rB;
  151. b2Vec2 m_localCenterA;
  152. b2Vec2 m_localCenterB;
  153. float m_invMassA;
  154. float m_invMassB;
  155. float m_invIA;
  156. float m_invIB;
  157. b2Mat22 m_K;
  158. float m_angle;
  159. float m_axialMass;
  160. };
  161. inline float b2RevoluteJoint::GetMotorSpeed() const
  162. {
  163. return m_motorSpeed;
  164. }
  165. #endif