b2_wheel_joint.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_WHEEL_JOINT_H
  19. #define B2_WHEEL_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Wheel 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 b2WheelJointDef : public b2JointDef
  29. {
  30. b2WheelJointDef()
  31. {
  32. type = e_wheelJoint;
  33. localAnchorA.SetZero();
  34. localAnchorB.SetZero();
  35. localAxisA.Set(1.0f, 0.0f);
  36. enableLimit = false;
  37. lowerTranslation = 0.0f;
  38. upperTranslation = 0.0f;
  39. enableMotor = false;
  40. maxMotorTorque = 0.0f;
  41. motorSpeed = 0.0f;
  42. stiffness = 0.0f;
  43. damping = 0.0f;
  44. }
  45. /// Initialize the bodies, anchors, axis, and reference angle using the world
  46. /// anchor and world axis.
  47. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
  48. /// The local anchor point relative to bodyA's origin.
  49. b2Vec2 localAnchorA;
  50. /// The local anchor point relative to bodyB's origin.
  51. b2Vec2 localAnchorB;
  52. /// The local translation axis in bodyA.
  53. b2Vec2 localAxisA;
  54. /// Enable/disable the joint limit.
  55. bool enableLimit;
  56. /// The lower translation limit, usually in meters.
  57. float lowerTranslation;
  58. /// The upper translation limit, usually in meters.
  59. float upperTranslation;
  60. /// Enable/disable the joint motor.
  61. bool enableMotor;
  62. /// The maximum motor torque, usually in N-m.
  63. float maxMotorTorque;
  64. /// The desired motor speed in radians per second.
  65. float motorSpeed;
  66. /// Suspension stiffness. Typically in units N/m.
  67. float stiffness;
  68. /// Suspension damping. Typically in units of N*s/m.
  69. float damping;
  70. };
  71. /// A wheel joint. This joint provides two degrees of freedom: translation
  72. /// along an axis fixed in bodyA and rotation in the plane. In other words, it is a point to
  73. /// line constraint with a rotational motor and a linear spring/damper. The spring/damper is
  74. /// initialized upon creation. This joint is designed for vehicle suspensions.
  75. class B2_API b2WheelJoint : public b2Joint
  76. {
  77. public:
  78. b2Vec2 GetAnchorA() const override;
  79. b2Vec2 GetAnchorB() const override;
  80. b2Vec2 GetReactionForce(float inv_dt) const override;
  81. float GetReactionTorque(float inv_dt) const override;
  82. /// The local anchor point relative to bodyA's origin.
  83. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  84. /// The local anchor point relative to bodyB's origin.
  85. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  86. /// The local joint axis relative to bodyA.
  87. const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
  88. /// Get the current joint translation, usually in meters.
  89. float GetJointTranslation() const;
  90. /// Get the current joint linear speed, usually in meters per second.
  91. float GetJointLinearSpeed() const;
  92. /// Get the current joint angle in radians.
  93. float GetJointAngle() const;
  94. /// Get the current joint angular speed in radians per second.
  95. float GetJointAngularSpeed() const;
  96. /// Is the joint limit enabled?
  97. bool IsLimitEnabled() const;
  98. /// Enable/disable the joint translation limit.
  99. void EnableLimit(bool flag);
  100. /// Get the lower joint translation limit, usually in meters.
  101. float GetLowerLimit() const;
  102. /// Get the upper joint translation limit, usually in meters.
  103. float GetUpperLimit() const;
  104. /// Set the joint translation limits, usually in meters.
  105. void SetLimits(float lower, float upper);
  106. /// Is the joint motor enabled?
  107. bool IsMotorEnabled() const;
  108. /// Enable/disable the joint motor.
  109. void EnableMotor(bool flag);
  110. /// Set the motor speed, usually in radians per second.
  111. void SetMotorSpeed(float speed);
  112. /// Get the motor speed, usually in radians per second.
  113. float GetMotorSpeed() const;
  114. /// Set/Get the maximum motor force, usually in N-m.
  115. void SetMaxMotorTorque(float torque);
  116. float GetMaxMotorTorque() const;
  117. /// Get the current motor torque given the inverse time step, usually in N-m.
  118. float GetMotorTorque(float inv_dt) const;
  119. /// Access spring stiffness
  120. void SetStiffness(float stiffness);
  121. float GetStiffness() const;
  122. /// Access damping
  123. void SetDamping(float damping);
  124. float GetDamping() const;
  125. /// Dump to b2Log
  126. void Dump() override;
  127. ///
  128. void Draw(b2Draw* draw) const override;
  129. protected:
  130. friend class b2Joint;
  131. b2WheelJoint(const b2WheelJointDef* def);
  132. void InitVelocityConstraints(const b2SolverData& data) override;
  133. void SolveVelocityConstraints(const b2SolverData& data) override;
  134. bool SolvePositionConstraints(const b2SolverData& data) override;
  135. b2Vec2 m_localAnchorA;
  136. b2Vec2 m_localAnchorB;
  137. b2Vec2 m_localXAxisA;
  138. b2Vec2 m_localYAxisA;
  139. float m_impulse;
  140. float m_motorImpulse;
  141. float m_springImpulse;
  142. float m_lowerImpulse;
  143. float m_upperImpulse;
  144. float m_translation;
  145. float m_lowerTranslation;
  146. float m_upperTranslation;
  147. float m_maxMotorTorque;
  148. float m_motorSpeed;
  149. bool m_enableLimit;
  150. bool m_enableMotor;
  151. float m_stiffness;
  152. float m_damping;
  153. // Solver temp
  154. int32 m_indexA;
  155. int32 m_indexB;
  156. b2Vec2 m_localCenterA;
  157. b2Vec2 m_localCenterB;
  158. float m_invMassA;
  159. float m_invMassB;
  160. float m_invIA;
  161. float m_invIB;
  162. b2Vec2 m_ax, m_ay;
  163. float m_sAx, m_sBx;
  164. float m_sAy, m_sBy;
  165. float m_mass;
  166. float m_motorMass;
  167. float m_axialMass;
  168. float m_springMass;
  169. float m_bias;
  170. float m_gamma;
  171. };
  172. inline float b2WheelJoint::GetMotorSpeed() const
  173. {
  174. return m_motorSpeed;
  175. }
  176. inline float b2WheelJoint::GetMaxMotorTorque() const
  177. {
  178. return m_maxMotorTorque;
  179. }
  180. #endif