b2_distance_joint.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_DISTANCE_JOINT_H
  19. #define B2_DISTANCE_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Distance joint definition. This requires defining an anchor point on both
  23. /// bodies and the non-zero distance of the distance joint. The definition uses
  24. /// local anchor points so that the initial configuration can violate the
  25. /// constraint slightly. This helps when saving and loading a game.
  26. struct B2_API b2DistanceJointDef : public b2JointDef
  27. {
  28. b2DistanceJointDef()
  29. {
  30. type = e_distanceJoint;
  31. localAnchorA.Set(0.0f, 0.0f);
  32. localAnchorB.Set(0.0f, 0.0f);
  33. length = 1.0f;
  34. minLength = 0.0f;
  35. maxLength = FLT_MAX;
  36. stiffness = 0.0f;
  37. damping = 0.0f;
  38. }
  39. /// Initialize the bodies, anchors, and rest length using world space anchors.
  40. /// The minimum and maximum lengths are set to the rest length.
  41. void Initialize(b2Body* bodyA, b2Body* bodyB,
  42. const b2Vec2& anchorA, const b2Vec2& anchorB);
  43. /// The local anchor point relative to bodyA's origin.
  44. b2Vec2 localAnchorA;
  45. /// The local anchor point relative to bodyB's origin.
  46. b2Vec2 localAnchorB;
  47. /// The rest length of this joint. Clamped to a stable minimum value.
  48. float length;
  49. /// Minimum length. Clamped to a stable minimum value.
  50. float minLength;
  51. /// Maximum length. Must be greater than or equal to the minimum length.
  52. float maxLength;
  53. /// The linear stiffness in N/m.
  54. float stiffness;
  55. /// The linear damping in N*s/m.
  56. float damping;
  57. };
  58. /// A distance joint constrains two points on two bodies to remain at a fixed
  59. /// distance from each other. You can view this as a massless, rigid rod.
  60. class B2_API b2DistanceJoint : public b2Joint
  61. {
  62. public:
  63. b2Vec2 GetAnchorA() const override;
  64. b2Vec2 GetAnchorB() const override;
  65. /// Get the reaction force given the inverse time step.
  66. /// Unit is N.
  67. b2Vec2 GetReactionForce(float inv_dt) const override;
  68. /// Get the reaction torque given the inverse time step.
  69. /// Unit is N*m. This is always zero for a distance joint.
  70. float GetReactionTorque(float inv_dt) const override;
  71. /// The local anchor point relative to bodyA's origin.
  72. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  73. /// The local anchor point relative to bodyB's origin.
  74. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  75. /// Get the rest length
  76. float GetLength() const { return m_length; }
  77. /// Set the rest length
  78. /// @returns clamped rest length
  79. float SetLength(float length);
  80. /// Get the minimum length
  81. float GetMinLength() const { return m_minLength; }
  82. /// Set the minimum length
  83. /// @returns the clamped minimum length
  84. float SetMinLength(float minLength);
  85. /// Get the maximum length
  86. float GetMaxLength() const { return m_maxLength; }
  87. /// Set the maximum length
  88. /// @returns the clamped maximum length
  89. float SetMaxLength(float maxLength);
  90. /// Get the current length
  91. float GetCurrentLength() const;
  92. /// Set/get the linear stiffness in N/m
  93. void SetStiffness(float stiffness) { m_stiffness = stiffness; }
  94. float GetStiffness() const { return m_stiffness; }
  95. /// Set/get linear damping in N*s/m
  96. void SetDamping(float damping) { m_damping = damping; }
  97. float GetDamping() const { return m_damping; }
  98. /// Dump joint to dmLog
  99. void Dump() override;
  100. ///
  101. void Draw(b2Draw* draw) const override;
  102. protected:
  103. friend class b2Joint;
  104. b2DistanceJoint(const b2DistanceJointDef* data);
  105. void InitVelocityConstraints(const b2SolverData& data) override;
  106. void SolveVelocityConstraints(const b2SolverData& data) override;
  107. bool SolvePositionConstraints(const b2SolverData& data) override;
  108. float m_stiffness;
  109. float m_damping;
  110. float m_bias;
  111. float m_length;
  112. float m_minLength;
  113. float m_maxLength;
  114. // Solver shared
  115. b2Vec2 m_localAnchorA;
  116. b2Vec2 m_localAnchorB;
  117. float m_gamma;
  118. float m_impulse;
  119. float m_lowerImpulse;
  120. float m_upperImpulse;
  121. // Solver temp
  122. int32 m_indexA;
  123. int32 m_indexB;
  124. b2Vec2 m_u;
  125. b2Vec2 m_rA;
  126. b2Vec2 m_rB;
  127. b2Vec2 m_localCenterA;
  128. b2Vec2 m_localCenterB;
  129. float m_currentLength;
  130. float m_invMassA;
  131. float m_invMassB;
  132. float m_invIA;
  133. float m_invIB;
  134. float m_softMass;
  135. float m_mass;
  136. };
  137. #endif