b2WheelJoint.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. In other words, it is a point to
  63. /// line constraint with a rotational motor and a linear spring/damper.
  64. /// This joint is designed for vehicle suspensions.
  65. class b2WheelJoint : public b2Joint
  66. {
  67. public:
  68. b2Vec2 GetAnchorA() const;
  69. b2Vec2 GetAnchorB() const;
  70. b2Vec2 GetReactionForce(float32 inv_dt) const;
  71. float32 GetReactionTorque(float32 inv_dt) const;
  72. /// The local anchor point relative to bodyA's origin.
  73. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  74. /// The local anchor point relative to bodyB's origin.
  75. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  76. /// The local joint axis relative to bodyA.
  77. const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
  78. /// Get the current joint translation, usually in meters.
  79. float32 GetJointTranslation() const;
  80. /// Get the current joint translation speed, usually in meters per second.
  81. float32 GetJointSpeed() const;
  82. /// Is the joint motor enabled?
  83. bool IsMotorEnabled() const;
  84. /// Enable/disable the joint motor.
  85. void EnableMotor(bool flag);
  86. /// Set the motor speed, usually in radians per second.
  87. void SetMotorSpeed(float32 speed);
  88. /// Get the motor speed, usually in radians per second.
  89. float32 GetMotorSpeed() const;
  90. /// Set/Get the maximum motor force, usually in N-m.
  91. void SetMaxMotorTorque(float32 torque);
  92. float32 GetMaxMotorTorque() const;
  93. /// Get the current motor torque given the inverse time step, usually in N-m.
  94. float32 GetMotorTorque(float32 inv_dt) const;
  95. /// Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring.
  96. void SetSpringFrequencyHz(float32 hz);
  97. float32 GetSpringFrequencyHz() const;
  98. /// Set/Get the spring damping ratio
  99. void SetSpringDampingRatio(float32 ratio);
  100. float32 GetSpringDampingRatio() const;
  101. /// Dump to b2Log
  102. void Dump();
  103. protected:
  104. friend class b2Joint;
  105. b2WheelJoint(const b2WheelJointDef* def);
  106. void InitVelocityConstraints(const b2SolverData& data);
  107. void SolveVelocityConstraints(const b2SolverData& data);
  108. bool SolvePositionConstraints(const b2SolverData& data);
  109. float32 m_frequencyHz;
  110. float32 m_dampingRatio;
  111. // Solver shared
  112. b2Vec2 m_localAnchorA;
  113. b2Vec2 m_localAnchorB;
  114. b2Vec2 m_localXAxisA;
  115. b2Vec2 m_localYAxisA;
  116. float32 m_impulse;
  117. float32 m_motorImpulse;
  118. float32 m_springImpulse;
  119. float32 m_maxMotorTorque;
  120. float32 m_motorSpeed;
  121. bool m_enableMotor;
  122. // Solver temp
  123. int32 m_indexA;
  124. int32 m_indexB;
  125. b2Vec2 m_localCenterA;
  126. b2Vec2 m_localCenterB;
  127. float32 m_invMassA;
  128. float32 m_invMassB;
  129. float32 m_invIA;
  130. float32 m_invIB;
  131. b2Vec2 m_ax, m_ay;
  132. float32 m_sAx, m_sBx;
  133. float32 m_sAy, m_sBy;
  134. float32 m_mass;
  135. float32 m_motorMass;
  136. float32 m_springMass;
  137. float32 m_bias;
  138. float32 m_gamma;
  139. };
  140. inline float32 b2WheelJoint::GetMotorSpeed() const
  141. {
  142. return m_motorSpeed;
  143. }
  144. inline float32 b2WheelJoint::GetMaxMotorTorque() const
  145. {
  146. return m_maxMotorTorque;
  147. }
  148. inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
  149. {
  150. m_frequencyHz = hz;
  151. }
  152. inline float32 b2WheelJoint::GetSpringFrequencyHz() const
  153. {
  154. return m_frequencyHz;
  155. }
  156. inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
  157. {
  158. m_dampingRatio = ratio;
  159. }
  160. inline float32 b2WheelJoint::GetSpringDampingRatio() const
  161. {
  162. return m_dampingRatio;
  163. }
  164. #endif