b2_weld_joint.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_WELD_JOINT_H
  19. #define B2_WELD_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Weld joint definition. You need to specify local anchor points
  23. /// where they are attached and the relative body angle. The position
  24. /// of the anchor points is important for computing the reaction torque.
  25. struct B2_API b2WeldJointDef : public b2JointDef
  26. {
  27. b2WeldJointDef()
  28. {
  29. type = e_weldJoint;
  30. localAnchorA.Set(0.0f, 0.0f);
  31. localAnchorB.Set(0.0f, 0.0f);
  32. referenceAngle = 0.0f;
  33. stiffness = 0.0f;
  34. damping = 0.0f;
  35. }
  36. /// Initialize the bodies, anchors, reference angle, stiffness, and damping.
  37. /// @param bodyA the first body connected by this joint
  38. /// @param bodyB the second body connected by this joint
  39. /// @param anchor the point of connection in world coordinates
  40. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
  41. /// The local anchor point relative to bodyA's origin.
  42. b2Vec2 localAnchorA;
  43. /// The local anchor point relative to bodyB's origin.
  44. b2Vec2 localAnchorB;
  45. /// The bodyB angle minus bodyA angle in the reference state (radians).
  46. float referenceAngle;
  47. /// The rotational stiffness in N*m
  48. /// Disable softness with a value of 0
  49. float stiffness;
  50. /// The rotational damping in N*m*s
  51. float damping;
  52. };
  53. /// A weld joint essentially glues two bodies together. A weld joint may
  54. /// distort somewhat because the island constraint solver is approximate.
  55. class B2_API b2WeldJoint : public b2Joint
  56. {
  57. public:
  58. b2Vec2 GetAnchorA() const override;
  59. b2Vec2 GetAnchorB() const override;
  60. b2Vec2 GetReactionForce(float inv_dt) const override;
  61. float GetReactionTorque(float inv_dt) const override;
  62. /// The local anchor point relative to bodyA's origin.
  63. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  64. /// The local anchor point relative to bodyB's origin.
  65. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  66. /// Get the reference angle.
  67. float GetReferenceAngle() const { return m_referenceAngle; }
  68. /// Set/get stiffness in N*m
  69. void SetStiffness(float stiffness) { m_stiffness = stiffness; }
  70. float GetStiffness() const { return m_stiffness; }
  71. /// Set/get damping in N*m*s
  72. void SetDamping(float damping) { m_damping = damping; }
  73. float GetDamping() const { return m_damping; }
  74. /// Dump to b2Log
  75. void Dump() override;
  76. protected:
  77. friend class b2Joint;
  78. b2WeldJoint(const b2WeldJointDef* def);
  79. void InitVelocityConstraints(const b2SolverData& data) override;
  80. void SolveVelocityConstraints(const b2SolverData& data) override;
  81. bool SolvePositionConstraints(const b2SolverData& data) override;
  82. float m_stiffness;
  83. float m_damping;
  84. float m_bias;
  85. // Solver shared
  86. b2Vec2 m_localAnchorA;
  87. b2Vec2 m_localAnchorB;
  88. float m_referenceAngle;
  89. float m_gamma;
  90. b2Vec3 m_impulse;
  91. // Solver temp
  92. int32 m_indexA;
  93. int32 m_indexB;
  94. b2Vec2 m_rA;
  95. b2Vec2 m_rB;
  96. b2Vec2 m_localCenterA;
  97. b2Vec2 m_localCenterB;
  98. float m_invMassA;
  99. float m_invMassB;
  100. float m_invIA;
  101. float m_invIB;
  102. b2Mat33 m_mass;
  103. };
  104. #endif