b2_prismatic_joint.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_PRISMATIC_JOINT_H
  19. #define B2_PRISMATIC_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Prismatic joint definition. This requires defining a line of
  23. /// motion using an axis and an anchor point. The definition uses local
  24. /// anchor points and a local axis so that the initial configuration
  25. /// can violate the constraint slightly. The joint translation is zero
  26. /// when the local anchor points coincide in world space. Using local
  27. /// anchors and a local axis helps when saving and loading a game.
  28. struct B2_API b2PrismaticJointDef : public b2JointDef
  29. {
  30. b2PrismaticJointDef()
  31. {
  32. type = e_prismaticJoint;
  33. localAnchorA.SetZero();
  34. localAnchorB.SetZero();
  35. localAxisA.Set(1.0f, 0.0f);
  36. referenceAngle = 0.0f;
  37. enableLimit = false;
  38. lowerTranslation = 0.0f;
  39. upperTranslation = 0.0f;
  40. enableMotor = false;
  41. maxMotorForce = 0.0f;
  42. motorSpeed = 0.0f;
  43. }
  44. /// Initialize the bodies, anchors, axis, and reference angle using the world
  45. /// anchor and unit world axis.
  46. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
  47. /// The local anchor point relative to bodyA's origin.
  48. b2Vec2 localAnchorA;
  49. /// The local anchor point relative to bodyB's origin.
  50. b2Vec2 localAnchorB;
  51. /// The local translation unit axis in bodyA.
  52. b2Vec2 localAxisA;
  53. /// The constrained angle between the bodies: bodyB_angle - bodyA_angle.
  54. float referenceAngle;
  55. /// Enable/disable the joint limit.
  56. bool enableLimit;
  57. /// The lower translation limit, usually in meters.
  58. float lowerTranslation;
  59. /// The upper translation limit, usually in meters.
  60. float upperTranslation;
  61. /// Enable/disable the joint motor.
  62. bool enableMotor;
  63. /// The maximum motor torque, usually in N-m.
  64. float maxMotorForce;
  65. /// The desired motor speed in radians per second.
  66. float motorSpeed;
  67. };
  68. /// A prismatic joint. This joint provides one degree of freedom: translation
  69. /// along an axis fixed in bodyA. Relative rotation is prevented. You can
  70. /// use a joint limit to restrict the range of motion and a joint motor to
  71. /// drive the motion or to model joint friction.
  72. class B2_API b2PrismaticJoint : public b2Joint
  73. {
  74. public:
  75. b2Vec2 GetAnchorA() const override;
  76. b2Vec2 GetAnchorB() const override;
  77. b2Vec2 GetReactionForce(float inv_dt) const override;
  78. float GetReactionTorque(float inv_dt) const override;
  79. /// The local anchor point relative to bodyA's origin.
  80. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  81. /// The local anchor point relative to bodyB's origin.
  82. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  83. /// The local joint axis relative to bodyA.
  84. const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
  85. /// Get the reference angle.
  86. float GetReferenceAngle() const { return m_referenceAngle; }
  87. /// Get the current joint translation, usually in meters.
  88. float GetJointTranslation() const;
  89. /// Get the current joint translation speed, usually in meters 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, usually in meters.
  96. float GetLowerLimit() const;
  97. /// Get the upper joint limit, usually in meters.
  98. float GetUpperLimit() const;
  99. /// Set the joint limits, usually in meters.
  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, usually in meters per second.
  106. void SetMotorSpeed(float speed);
  107. /// Get the motor speed, usually in meters per second.
  108. float GetMotorSpeed() const;
  109. /// Set the maximum motor force, usually in N.
  110. void SetMaxMotorForce(float force);
  111. float GetMaxMotorForce() const { return m_maxMotorForce; }
  112. /// Get the current motor force given the inverse time step, usually in N.
  113. float GetMotorForce(float inv_dt) const;
  114. /// Dump to b2Log
  115. void Dump() override;
  116. ///
  117. void Draw(b2Draw* draw) const override;
  118. protected:
  119. friend class b2Joint;
  120. friend class b2GearJoint;
  121. b2PrismaticJoint(const b2PrismaticJointDef* def);
  122. void InitVelocityConstraints(const b2SolverData& data) override;
  123. void SolveVelocityConstraints(const b2SolverData& data) override;
  124. bool SolvePositionConstraints(const b2SolverData& data) override;
  125. b2Vec2 m_localAnchorA;
  126. b2Vec2 m_localAnchorB;
  127. b2Vec2 m_localXAxisA;
  128. b2Vec2 m_localYAxisA;
  129. float m_referenceAngle;
  130. b2Vec2 m_impulse;
  131. float m_motorImpulse;
  132. float m_lowerImpulse;
  133. float m_upperImpulse;
  134. float m_lowerTranslation;
  135. float m_upperTranslation;
  136. float m_maxMotorForce;
  137. float m_motorSpeed;
  138. bool m_enableLimit;
  139. bool m_enableMotor;
  140. // Solver temp
  141. int32 m_indexA;
  142. int32 m_indexB;
  143. b2Vec2 m_localCenterA;
  144. b2Vec2 m_localCenterB;
  145. float m_invMassA;
  146. float m_invMassB;
  147. float m_invIA;
  148. float m_invIB;
  149. b2Vec2 m_axis, m_perp;
  150. float m_s1, m_s2;
  151. float m_a1, m_a2;
  152. b2Mat22 m_K;
  153. float m_translation;
  154. float m_axialMass;
  155. };
  156. inline float b2PrismaticJoint::GetMotorSpeed() const
  157. {
  158. return m_motorSpeed;
  159. }
  160. #endif