b2PulleyJoint.h 4.3 KB

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