b2RopeJoint.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_ROPE_JOINT_H
  19. #define B2_ROPE_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Rope joint definition. This requires two body anchor points and
  22. /// a maximum lengths.
  23. /// Note: by default the connected objects will not collide.
  24. /// see collideConnected in b2JointDef.
  25. struct b2RopeJointDef : public b2JointDef
  26. {
  27. b2RopeJointDef()
  28. {
  29. type = e_ropeJoint;
  30. localAnchorA.Set(-1.0f, 0.0f);
  31. localAnchorB.Set(1.0f, 0.0f);
  32. maxLength = 0.0f;
  33. }
  34. /// The local anchor point relative to bodyA's origin.
  35. b2Vec2 localAnchorA;
  36. /// The local anchor point relative to bodyB's origin.
  37. b2Vec2 localAnchorB;
  38. /// The maximum length of the rope.
  39. /// Warning: this must be larger than b2_linearSlop or
  40. /// the joint will have no effect.
  41. float32 maxLength;
  42. };
  43. /// A rope joint enforces a maximum distance between two points
  44. /// on two bodies. It has no other effect.
  45. /// Warning: if you attempt to change the maximum length during
  46. /// the simulation you will get some non-physical behavior.
  47. /// A model that would allow you to dynamically modify the length
  48. /// would have some sponginess, so I chose not to implement it
  49. /// that way. See b2DistanceJoint if you want to dynamically
  50. /// control length.
  51. class b2RopeJoint : public b2Joint
  52. {
  53. public:
  54. b2Vec2 GetAnchorA() const;
  55. b2Vec2 GetAnchorB() const;
  56. b2Vec2 GetReactionForce(float32 inv_dt) const;
  57. float32 GetReactionTorque(float32 inv_dt) const;
  58. /// The local anchor point relative to bodyA's origin.
  59. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  60. /// The local anchor point relative to bodyB's origin.
  61. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  62. /// Set/Get the maximum length of the rope.
  63. void SetMaxLength(float32 length) { m_maxLength = length; }
  64. float32 GetMaxLength() const;
  65. b2LimitState GetLimitState() const;
  66. /// Dump joint to dmLog
  67. void Dump();
  68. protected:
  69. friend class b2Joint;
  70. b2RopeJoint(const b2RopeJointDef* data);
  71. void InitVelocityConstraints(const b2SolverData& data);
  72. void SolveVelocityConstraints(const b2SolverData& data);
  73. bool SolvePositionConstraints(const b2SolverData& data);
  74. // Solver shared
  75. b2Vec2 m_localAnchorA;
  76. b2Vec2 m_localAnchorB;
  77. float32 m_maxLength;
  78. float32 m_length;
  79. float32 m_impulse;
  80. // Solver temp
  81. int32 m_indexA;
  82. int32 m_indexB;
  83. b2Vec2 m_u;
  84. b2Vec2 m_rA;
  85. b2Vec2 m_rB;
  86. b2Vec2 m_localCenterA;
  87. b2Vec2 m_localCenterB;
  88. float32 m_invMassA;
  89. float32 m_invMassB;
  90. float32 m_invIA;
  91. float32 m_invIB;
  92. float32 m_mass;
  93. b2LimitState m_state;
  94. };
  95. #endif