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.

144 lines
4.2KB

  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 "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. /// Dump joint to dmLog
  84. void Dump();
  85. protected:
  86. friend class b2Joint;
  87. b2PulleyJoint(const b2PulleyJointDef* data);
  88. void InitVelocityConstraints(const b2SolverData& data);
  89. void SolveVelocityConstraints(const b2SolverData& data);
  90. bool SolvePositionConstraints(const b2SolverData& data);
  91. b2Vec2 m_groundAnchorA;
  92. b2Vec2 m_groundAnchorB;
  93. float32 m_lengthA;
  94. float32 m_lengthB;
  95. // Solver shared
  96. b2Vec2 m_localAnchorA;
  97. b2Vec2 m_localAnchorB;
  98. float32 m_constant;
  99. float32 m_ratio;
  100. float32 m_impulse;
  101. // Solver temp
  102. juce::int32 m_indexA;
  103. juce::int32 m_indexB;
  104. b2Vec2 m_uA;
  105. b2Vec2 m_uB;
  106. b2Vec2 m_rA;
  107. b2Vec2 m_rB;
  108. b2Vec2 m_localCenterA;
  109. b2Vec2 m_localCenterB;
  110. float32 m_invMassA;
  111. float32 m_invMassB;
  112. float32 m_invIA;
  113. float32 m_invIB;
  114. float32 m_mass;
  115. };
  116. #endif