b2DistanceJoint.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2007 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_DISTANCE_JOINT_H
  19. #define B2_DISTANCE_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Distance joint definition. This requires defining an
  22. /// anchor point on both bodies and the non-zero length of the
  23. /// distance joint. The definition uses local anchor points
  24. /// so that the initial configuration can violate the constraint
  25. /// slightly. This helps when saving and loading a game.
  26. /// @warning Do not use a zero or short length.
  27. struct b2DistanceJointDef : public b2JointDef
  28. {
  29. b2DistanceJointDef()
  30. {
  31. type = e_distanceJoint;
  32. localAnchorA.Set(0.0f, 0.0f);
  33. localAnchorB.Set(0.0f, 0.0f);
  34. length = 1.0f;
  35. frequencyHz = 0.0f;
  36. dampingRatio = 0.0f;
  37. }
  38. /// Initialize the bodies, anchors, and length using the world
  39. /// anchors.
  40. void Initialize(b2Body* bodyA, b2Body* bodyB,
  41. const b2Vec2& anchorA, const b2Vec2& anchorB);
  42. /// The local anchor point relative to bodyA's origin.
  43. b2Vec2 localAnchorA;
  44. /// The local anchor point relative to bodyB's origin.
  45. b2Vec2 localAnchorB;
  46. /// The natural length between the anchor points.
  47. float32 length;
  48. /// The mass-spring-damper frequency in Hertz. A value of 0
  49. /// disables softness.
  50. float32 frequencyHz;
  51. /// The damping ratio. 0 = no damping, 1 = critical damping.
  52. float32 dampingRatio;
  53. };
  54. /// A distance joint constrains two points on two bodies
  55. /// to remain at a fixed distance from each other. You can view
  56. /// this as a massless, rigid rod.
  57. class b2DistanceJoint : public b2Joint
  58. {
  59. public:
  60. b2Vec2 GetAnchorA() const;
  61. b2Vec2 GetAnchorB() const;
  62. /// Get the reaction force given the inverse time step.
  63. /// Unit is N.
  64. b2Vec2 GetReactionForce(float32 inv_dt) const;
  65. /// Get the reaction torque given the inverse time step.
  66. /// Unit is N*m. This is always zero for a distance joint.
  67. float32 GetReactionTorque(float32 inv_dt) const;
  68. /// The local anchor point relative to bodyA's origin.
  69. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  70. /// The local anchor point relative to bodyB's origin.
  71. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  72. /// Set/get the natural length.
  73. /// Manipulating the length can lead to non-physical behavior when the frequency is zero.
  74. void SetLength(float32 length);
  75. float32 GetLength() const;
  76. /// Set/get frequency in Hz.
  77. void SetFrequency(float32 hz);
  78. float32 GetFrequency() const;
  79. /// Set/get damping ratio.
  80. void SetDampingRatio(float32 ratio);
  81. float32 GetDampingRatio() const;
  82. /// Dump joint to dmLog
  83. void Dump();
  84. protected:
  85. friend class b2Joint;
  86. b2DistanceJoint(const b2DistanceJointDef* data);
  87. void InitVelocityConstraints(const b2SolverData& data);
  88. void SolveVelocityConstraints(const b2SolverData& data);
  89. bool SolvePositionConstraints(const b2SolverData& data);
  90. float32 m_frequencyHz;
  91. float32 m_dampingRatio;
  92. float32 m_bias;
  93. // Solver shared
  94. b2Vec2 m_localAnchorA;
  95. b2Vec2 m_localAnchorB;
  96. float32 m_gamma;
  97. float32 m_impulse;
  98. float32 m_length;
  99. // Solver temp
  100. int32 m_indexA;
  101. int32 m_indexB;
  102. b2Vec2 m_u;
  103. b2Vec2 m_rA;
  104. b2Vec2 m_rB;
  105. b2Vec2 m_localCenterA;
  106. b2Vec2 m_localCenterB;
  107. float32 m_invMassA;
  108. float32 m_invMassB;
  109. float32 m_invIA;
  110. float32 m_invIB;
  111. float32 m_mass;
  112. };
  113. inline void b2DistanceJoint::SetLength(float32 length)
  114. {
  115. m_length = length;
  116. }
  117. inline float32 b2DistanceJoint::GetLength() const
  118. {
  119. return m_length;
  120. }
  121. inline void b2DistanceJoint::SetFrequency(float32 hz)
  122. {
  123. m_frequencyHz = hz;
  124. }
  125. inline float32 b2DistanceJoint::GetFrequency() const
  126. {
  127. return m_frequencyHz;
  128. }
  129. inline void b2DistanceJoint::SetDampingRatio(float32 ratio)
  130. {
  131. m_dampingRatio = ratio;
  132. }
  133. inline float32 b2DistanceJoint::GetDampingRatio() const
  134. {
  135. return m_dampingRatio;
  136. }
  137. #endif