The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.0KB

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