b2_pulley_joint.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_PULLEY_JOINT_H
  19. #define B2_PULLEY_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. const float b2_minPulleyLength = 2.0f;
  23. /// Pulley joint definition. This requires two ground anchors,
  24. /// two dynamic body anchor points, and a pulley ratio.
  25. struct B2_API b2PulleyJointDef : public b2JointDef
  26. {
  27. b2PulleyJointDef()
  28. {
  29. type = e_pulleyJoint;
  30. groundAnchorA.Set(-1.0f, 1.0f);
  31. groundAnchorB.Set(1.0f, 1.0f);
  32. localAnchorA.Set(-1.0f, 0.0f);
  33. localAnchorB.Set(1.0f, 0.0f);
  34. lengthA = 0.0f;
  35. lengthB = 0.0f;
  36. ratio = 1.0f;
  37. collideConnected = true;
  38. }
  39. /// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
  40. void Initialize(b2Body* bodyA, b2Body* bodyB,
  41. const b2Vec2& groundAnchorA, const b2Vec2& groundAnchorB,
  42. const b2Vec2& anchorA, const b2Vec2& anchorB,
  43. float ratio);
  44. /// The first ground anchor in world coordinates. This point never moves.
  45. b2Vec2 groundAnchorA;
  46. /// The second ground anchor in world coordinates. This point never moves.
  47. b2Vec2 groundAnchorB;
  48. /// The local anchor point relative to bodyA's origin.
  49. b2Vec2 localAnchorA;
  50. /// The local anchor point relative to bodyB's origin.
  51. b2Vec2 localAnchorB;
  52. /// The a reference length for the segment attached to bodyA.
  53. float lengthA;
  54. /// The a reference length for the segment attached to bodyB.
  55. float lengthB;
  56. /// The pulley ratio, used to simulate a block-and-tackle.
  57. float ratio;
  58. };
  59. /// The pulley joint is connected to two bodies and two fixed ground points.
  60. /// The pulley supports a ratio such that:
  61. /// length1 + ratio * length2 <= constant
  62. /// Yes, the force transmitted is scaled by the ratio.
  63. /// Warning: the pulley joint can get a bit squirrelly by itself. They often
  64. /// work better when combined with prismatic joints. You should also cover the
  65. /// the anchor points with static shapes to prevent one side from going to
  66. /// zero length.
  67. class B2_API b2PulleyJoint : public b2Joint
  68. {
  69. public:
  70. b2Vec2 GetAnchorA() const override;
  71. b2Vec2 GetAnchorB() const override;
  72. b2Vec2 GetReactionForce(float inv_dt) const override;
  73. float GetReactionTorque(float inv_dt) const override;
  74. /// Get the first ground anchor.
  75. b2Vec2 GetGroundAnchorA() const;
  76. /// Get the second ground anchor.
  77. b2Vec2 GetGroundAnchorB() const;
  78. /// Get the current length of the segment attached to bodyA.
  79. float GetLengthA() const;
  80. /// Get the current length of the segment attached to bodyB.
  81. float GetLengthB() const;
  82. /// Get the pulley ratio.
  83. float GetRatio() const;
  84. /// Get the current length of the segment attached to bodyA.
  85. float GetCurrentLengthA() const;
  86. /// Get the current length of the segment attached to bodyB.
  87. float GetCurrentLengthB() const;
  88. /// Dump joint to dmLog
  89. void Dump() override;
  90. /// Implement b2Joint::ShiftOrigin
  91. void ShiftOrigin(const b2Vec2& newOrigin) override;
  92. protected:
  93. friend class b2Joint;
  94. b2PulleyJoint(const b2PulleyJointDef* data);
  95. void InitVelocityConstraints(const b2SolverData& data) override;
  96. void SolveVelocityConstraints(const b2SolverData& data) override;
  97. bool SolvePositionConstraints(const b2SolverData& data) override;
  98. b2Vec2 m_groundAnchorA;
  99. b2Vec2 m_groundAnchorB;
  100. float m_lengthA;
  101. float m_lengthB;
  102. // Solver shared
  103. b2Vec2 m_localAnchorA;
  104. b2Vec2 m_localAnchorB;
  105. float m_constant;
  106. float m_ratio;
  107. float m_impulse;
  108. // Solver temp
  109. int32 m_indexA;
  110. int32 m_indexB;
  111. b2Vec2 m_uA;
  112. b2Vec2 m_uB;
  113. b2Vec2 m_rA;
  114. b2Vec2 m_rB;
  115. b2Vec2 m_localCenterA;
  116. b2Vec2 m_localCenterB;
  117. float m_invMassA;
  118. float m_invMassB;
  119. float m_invIA;
  120. float m_invIB;
  121. float m_mass;
  122. };
  123. #endif