b2PulleyJoint.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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, max lengths for each side,
  24. /// and a pulley ratio.
  25. struct b2PulleyJointDef : public b2JointDef
  26. {
  27. b2PulleyJointDef()
  28. {
  29. type = e_pulleyJoint;
  30. groundAnchor1.Set(-1.0f, 1.0f);
  31. groundAnchor2.Set(1.0f, 1.0f);
  32. localAnchor1.Set(-1.0f, 0.0f);
  33. localAnchor2.Set(1.0f, 0.0f);
  34. length1 = 0.0f;
  35. maxLength1 = 0.0f;
  36. length2 = 0.0f;
  37. maxLength2 = 0.0f;
  38. ratio = 1.0f;
  39. collideConnected = true;
  40. }
  41. /// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
  42. void Initialize(b2Body* body1, b2Body* body2,
  43. const b2Vec2& groundAnchor1, const b2Vec2& groundAnchor2,
  44. const b2Vec2& anchor1, const b2Vec2& anchor2,
  45. float32 ratio);
  46. /// The first ground anchor in world coordinates. This point never moves.
  47. b2Vec2 groundAnchor1;
  48. /// The second ground anchor in world coordinates. This point never moves.
  49. b2Vec2 groundAnchor2;
  50. /// The local anchor point relative to body1's origin.
  51. b2Vec2 localAnchor1;
  52. /// The local anchor point relative to body2's origin.
  53. b2Vec2 localAnchor2;
  54. /// The a reference length for the segment attached to body1.
  55. float32 length1;
  56. /// The maximum length of the segment attached to body1.
  57. float32 maxLength1;
  58. /// The a reference length for the segment attached to body2.
  59. float32 length2;
  60. /// The maximum length of the segment attached to body2.
  61. float32 maxLength2;
  62. /// The pulley ratio, used to simulate a block-and-tackle.
  63. float32 ratio;
  64. };
  65. /// The pulley joint is connected to two bodies and two fixed ground points.
  66. /// The pulley supports a ratio such that:
  67. /// length1 + ratio * length2 <= constant
  68. /// Yes, the force transmitted is scaled by the ratio.
  69. /// The pulley also enforces a maximum length limit on both sides. This is
  70. /// useful to prevent one side of the pulley hitting the top.
  71. class b2PulleyJoint : public b2Joint
  72. {
  73. public:
  74. b2Vec2 GetAnchor1() const;
  75. b2Vec2 GetAnchor2() const;
  76. b2Vec2 GetReactionForce(float32 inv_dt) const;
  77. float32 GetReactionTorque(float32 inv_dt) const;
  78. /// Get the first ground anchor.
  79. b2Vec2 GetGroundAnchor1() const;
  80. /// Get the second ground anchor.
  81. b2Vec2 GetGroundAnchor2() const;
  82. /// Get the current length of the segment attached to body1.
  83. float32 GetLength1() const;
  84. /// Get the current length of the segment attached to body2.
  85. float32 GetLength2() const;
  86. /// Get the pulley ratio.
  87. float32 GetRatio() const;
  88. //--------------- Internals Below -------------------
  89. b2PulleyJoint(const b2PulleyJointDef* data);
  90. void InitVelocityConstraints(const b2TimeStep& step);
  91. void SolveVelocityConstraints(const b2TimeStep& step);
  92. bool SolvePositionConstraints(float32 baumgarte);
  93. b2Body* m_ground;
  94. b2Vec2 m_groundAnchor1;
  95. b2Vec2 m_groundAnchor2;
  96. b2Vec2 m_localAnchor1;
  97. b2Vec2 m_localAnchor2;
  98. b2Vec2 m_u1;
  99. b2Vec2 m_u2;
  100. float32 m_constant;
  101. float32 m_ratio;
  102. float32 m_maxLength1;
  103. float32 m_maxLength2;
  104. // Effective masses
  105. float32 m_pulleyMass;
  106. float32 m_limitMass1;
  107. float32 m_limitMass2;
  108. // Impulses for accumulation/warm starting.
  109. float32 m_impulse;
  110. float32 m_limitImpulse1;
  111. float32 m_limitImpulse2;
  112. b2LimitState m_state;
  113. b2LimitState m_limitState1;
  114. b2LimitState m_limitState2;
  115. };
  116. #endif