b2WeldJoint.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_WELD_JOINT_H
  19. #define B2_WELD_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Weld joint definition. You need to specify local anchor points
  22. /// where they are attached and the relative body angle. The position
  23. /// of the anchor points is important for computing the reaction torque.
  24. struct b2WeldJointDef : public b2JointDef
  25. {
  26. b2WeldJointDef()
  27. {
  28. type = e_weldJoint;
  29. localAnchorA.Set(0.0f, 0.0f);
  30. localAnchorB.Set(0.0f, 0.0f);
  31. referenceAngle = 0.0f;
  32. frequencyHz = 0.0f;
  33. dampingRatio = 0.0f;
  34. }
  35. /// Initialize the bodies, anchors, and reference angle using a world
  36. /// anchor point.
  37. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
  38. /// The local anchor point relative to bodyA's origin.
  39. b2Vec2 localAnchorA;
  40. /// The local anchor point relative to bodyB's origin.
  41. b2Vec2 localAnchorB;
  42. /// The bodyB angle minus bodyA angle in the reference state (radians).
  43. float32 referenceAngle;
  44. /// The mass-spring-damper frequency in Hertz. Rotation only.
  45. /// Disable softness with a value of 0.
  46. float32 frequencyHz;
  47. /// The damping ratio. 0 = no damping, 1 = critical damping.
  48. float32 dampingRatio;
  49. };
  50. /// A weld joint essentially glues two bodies together. A weld joint may
  51. /// distort somewhat because the island constraint solver is approximate.
  52. class b2WeldJoint : public b2Joint
  53. {
  54. public:
  55. b2Vec2 GetAnchorA() const;
  56. b2Vec2 GetAnchorB() const;
  57. b2Vec2 GetReactionForce(float32 inv_dt) const;
  58. float32 GetReactionTorque(float32 inv_dt) const;
  59. /// The local anchor point relative to bodyA's origin.
  60. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  61. /// The local anchor point relative to bodyB's origin.
  62. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  63. /// Get the reference angle.
  64. float32 GetReferenceAngle() const { return m_referenceAngle; }
  65. /// Set/get frequency in Hz.
  66. void SetFrequency(float32 hz) { m_frequencyHz = hz; }
  67. float32 GetFrequency() const { return m_frequencyHz; }
  68. /// Set/get damping ratio.
  69. void SetDampingRatio(float32 ratio) { m_dampingRatio = ratio; }
  70. float32 GetDampingRatio() const { return m_dampingRatio; }
  71. /// Dump to b2Log
  72. void Dump();
  73. protected:
  74. friend class b2Joint;
  75. b2WeldJoint(const b2WeldJointDef* def);
  76. void InitVelocityConstraints(const b2SolverData& data);
  77. void SolveVelocityConstraints(const b2SolverData& data);
  78. bool SolvePositionConstraints(const b2SolverData& data);
  79. float32 m_frequencyHz;
  80. float32 m_dampingRatio;
  81. float32 m_bias;
  82. // Solver shared
  83. b2Vec2 m_localAnchorA;
  84. b2Vec2 m_localAnchorB;
  85. float32 m_referenceAngle;
  86. float32 m_gamma;
  87. b2Vec3 m_impulse;
  88. // Solver temp
  89. int32 m_indexA;
  90. int32 m_indexB;
  91. b2Vec2 m_rA;
  92. b2Vec2 m_rB;
  93. b2Vec2 m_localCenterA;
  94. b2Vec2 m_localCenterB;
  95. float32 m_invMassA;
  96. float32 m_invMassB;
  97. float32 m_invIA;
  98. float32 m_invIB;
  99. b2Mat33 m_mass;
  100. };
  101. #endif