b2RevoluteJoint.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_REVOLUTE_JOINT_H
  19. #define B2_REVOLUTE_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Revolute joint definition. This requires defining an
  22. /// anchor point where the bodies are joined. The definition
  23. /// uses local anchor points so that the initial configuration
  24. /// can violate the constraint slightly. You also need to
  25. /// specify the initial relative angle for joint limits. This
  26. /// helps when saving 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 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. float32 referenceAngle;
  56. /// A flag to enable joint limits.
  57. bool enableLimit;
  58. /// The lower angle for the joint limit (radians).
  59. float32 lowerAngle;
  60. /// The upper angle for the joint limit (radians).
  61. float32 upperAngle;
  62. /// A flag to enable the joint motor.
  63. bool enableMotor;
  64. /// The desired motor speed. Usually in radians per second.
  65. float32 motorSpeed;
  66. /// The maximum motor torque used to achieve the desired motor speed.
  67. /// Usually in N-m.
  68. float32 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 b2RevoluteJoint : public b2Joint
  77. {
  78. public:
  79. b2Vec2 GetAnchorA() const;
  80. b2Vec2 GetAnchorB() const;
  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. float32 GetReferenceAngle() const { return m_referenceAngle; }
  87. /// Get the current joint angle in radians.
  88. float32 GetJointAngle() const;
  89. /// Get the current joint angle speed in radians per second.
  90. float32 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. float32 GetLowerLimit() const;
  97. /// Get the upper joint limit in radians.
  98. float32 GetUpperLimit() const;
  99. /// Set the joint limits in radians.
  100. void SetLimits(float32 lower, float32 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(float32 speed);
  107. /// Get the motor speed in radians per second.
  108. float32 GetMotorSpeed() const;
  109. /// Set the maximum motor torque, usually in N-m.
  110. void SetMaxMotorTorque(float32 torque);
  111. float32 GetMaxMotorTorque() const { return m_maxMotorTorque; }
  112. /// Get the reaction force given the inverse time step.
  113. /// Unit is N.
  114. b2Vec2 GetReactionForce(float32 inv_dt) const;
  115. /// Get the reaction torque due to the joint limit given the inverse time step.
  116. /// Unit is N*m.
  117. float32 GetReactionTorque(float32 inv_dt) const;
  118. /// Get the current motor torque given the inverse time step.
  119. /// Unit is N*m.
  120. float32 GetMotorTorque(float32 inv_dt) const;
  121. /// Dump to b2Log.
  122. void Dump();
  123. protected:
  124. friend class b2Joint;
  125. friend class b2GearJoint;
  126. b2RevoluteJoint(const b2RevoluteJointDef* def);
  127. void InitVelocityConstraints(const b2SolverData& data);
  128. void SolveVelocityConstraints(const b2SolverData& data);
  129. bool SolvePositionConstraints(const b2SolverData& data);
  130. // Solver shared
  131. b2Vec2 m_localAnchorA;
  132. b2Vec2 m_localAnchorB;
  133. b2Vec3 m_impulse;
  134. float32 m_motorImpulse;
  135. bool m_enableMotor;
  136. float32 m_maxMotorTorque;
  137. float32 m_motorSpeed;
  138. bool m_enableLimit;
  139. float32 m_referenceAngle;
  140. float32 m_lowerAngle;
  141. float32 m_upperAngle;
  142. // Solver temp
  143. int32 m_indexA;
  144. int32 m_indexB;
  145. b2Vec2 m_rA;
  146. b2Vec2 m_rB;
  147. b2Vec2 m_localCenterA;
  148. b2Vec2 m_localCenterB;
  149. float32 m_invMassA;
  150. float32 m_invMassB;
  151. float32 m_invIA;
  152. float32 m_invIB;
  153. b2Mat33 m_mass; // effective mass for point-to-point constraint.
  154. float32 m_motorMass; // effective mass for motor/limit angular constraint.
  155. b2LimitState m_limitState;
  156. };
  157. inline float32 b2RevoluteJoint::GetMotorSpeed() const
  158. {
  159. return m_motorSpeed;
  160. }
  161. #endif