b2WheelJoint.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_WHEEL_JOINT_H
  19. #define B2_WHEEL_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Wheel joint definition. This requires defining a line of
  22. /// motion using an axis and an anchor point. The definition uses local
  23. /// anchor points and a local axis so that the initial configuration
  24. /// can violate the constraint slightly. The joint translation is zero
  25. /// when the local anchor points coincide in world space. Using local
  26. /// anchors and a local axis helps when saving and loading a game.
  27. struct b2WheelJointDef : public b2JointDef
  28. {
  29. b2WheelJointDef()
  30. {
  31. type = e_wheelJoint;
  32. localAnchorA.SetZero();
  33. localAnchorB.SetZero();
  34. localAxisA.Set(1.0f, 0.0f);
  35. enableMotor = false;
  36. maxMotorTorque = 0.0f;
  37. motorSpeed = 0.0f;
  38. frequencyHz = 2.0f;
  39. dampingRatio = 0.7f;
  40. }
  41. /// Initialize the bodies, anchors, axis, and reference angle using the world
  42. /// anchor and world axis.
  43. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
  44. /// The local anchor point relative to bodyA's origin.
  45. b2Vec2 localAnchorA;
  46. /// The local anchor point relative to bodyB's origin.
  47. b2Vec2 localAnchorB;
  48. /// The local translation axis in bodyA.
  49. b2Vec2 localAxisA;
  50. /// Enable/disable the joint motor.
  51. bool enableMotor;
  52. /// The maximum motor torque, usually in N-m.
  53. float32 maxMotorTorque;
  54. /// The desired motor speed in radians per second.
  55. float32 motorSpeed;
  56. /// Suspension frequency, zero indicates no suspension
  57. float32 frequencyHz;
  58. /// Suspension damping ratio, one indicates critical damping
  59. float32 dampingRatio;
  60. };
  61. /// A wheel joint. This joint provides two degrees of freedom: translation
  62. /// along an axis fixed in bodyA and rotation in the plane. You can use a
  63. /// joint limit to restrict the range of motion and a joint motor to drive
  64. /// the rotation or to model rotational friction.
  65. /// This joint is designed for vehicle suspensions.
  66. class b2WheelJoint : public b2Joint
  67. {
  68. public:
  69. b2Vec2 GetAnchorA() const;
  70. b2Vec2 GetAnchorB() const;
  71. b2Vec2 GetReactionForce(float32 inv_dt) const;
  72. float32 GetReactionTorque(float32 inv_dt) const;
  73. /// The local anchor point relative to bodyA's origin.
  74. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  75. /// The local anchor point relative to bodyB's origin.
  76. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  77. /// The local joint axis relative to bodyA.
  78. const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
  79. /// Get the current joint translation, usually in meters.
  80. float32 GetJointTranslation() const;
  81. /// Get the current joint translation speed, usually in meters per second.
  82. float32 GetJointSpeed() const;
  83. /// Is the joint motor enabled?
  84. bool IsMotorEnabled() const;
  85. /// Enable/disable the joint motor.
  86. void EnableMotor(bool flag);
  87. /// Set the motor speed, usually in radians per second.
  88. void SetMotorSpeed(float32 speed);
  89. /// Get the motor speed, usually in radians per second.
  90. float32 GetMotorSpeed() const;
  91. /// Set/Get the maximum motor force, usually in N-m.
  92. void SetMaxMotorTorque(float32 torque);
  93. float32 GetMaxMotorTorque() const;
  94. /// Get the current motor torque given the inverse time step, usually in N-m.
  95. float32 GetMotorTorque(float32 inv_dt) const;
  96. /// Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring.
  97. void SetSpringFrequencyHz(float32 hz);
  98. float32 GetSpringFrequencyHz() const;
  99. /// Set/Get the spring damping ratio
  100. void SetSpringDampingRatio(float32 ratio);
  101. float32 GetSpringDampingRatio() const;
  102. /// Dump to b2Log
  103. void Dump();
  104. protected:
  105. friend class b2Joint;
  106. b2WheelJoint(const b2WheelJointDef* def);
  107. void InitVelocityConstraints(const b2SolverData& data);
  108. void SolveVelocityConstraints(const b2SolverData& data);
  109. bool SolvePositionConstraints(const b2SolverData& data);
  110. float32 m_frequencyHz;
  111. float32 m_dampingRatio;
  112. // Solver shared
  113. b2Vec2 m_localAnchorA;
  114. b2Vec2 m_localAnchorB;
  115. b2Vec2 m_localXAxisA;
  116. b2Vec2 m_localYAxisA;
  117. float32 m_impulse;
  118. float32 m_motorImpulse;
  119. float32 m_springImpulse;
  120. float32 m_maxMotorTorque;
  121. float32 m_motorSpeed;
  122. bool m_enableMotor;
  123. // Solver temp
  124. int32 m_indexA;
  125. int32 m_indexB;
  126. b2Vec2 m_localCenterA;
  127. b2Vec2 m_localCenterB;
  128. float32 m_invMassA;
  129. float32 m_invMassB;
  130. float32 m_invIA;
  131. float32 m_invIB;
  132. b2Vec2 m_ax, m_ay;
  133. float32 m_sAx, m_sBx;
  134. float32 m_sAy, m_sBy;
  135. float32 m_mass;
  136. float32 m_motorMass;
  137. float32 m_springMass;
  138. float32 m_bias;
  139. float32 m_gamma;
  140. };
  141. inline float32 b2WheelJoint::GetMotorSpeed() const
  142. {
  143. return m_motorSpeed;
  144. }
  145. inline float32 b2WheelJoint::GetMaxMotorTorque() const
  146. {
  147. return m_maxMotorTorque;
  148. }
  149. inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
  150. {
  151. m_frequencyHz = hz;
  152. }
  153. inline float32 b2WheelJoint::GetSpringFrequencyHz() const
  154. {
  155. return m_frequencyHz;
  156. }
  157. inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
  158. {
  159. m_dampingRatio = ratio;
  160. }
  161. inline float32 b2WheelJoint::GetSpringDampingRatio() const
  162. {
  163. return m_dampingRatio;
  164. }
  165. #endif