b2_friction_joint.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_FRICTION_JOINT_H
  19. #define B2_FRICTION_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Friction joint definition.
  23. struct B2_API b2FrictionJointDef : public b2JointDef
  24. {
  25. b2FrictionJointDef()
  26. {
  27. type = e_frictionJoint;
  28. localAnchorA.SetZero();
  29. localAnchorB.SetZero();
  30. maxForce = 0.0f;
  31. maxTorque = 0.0f;
  32. }
  33. /// Initialize the bodies, anchors, axis, and reference angle using the world
  34. /// anchor and world axis.
  35. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
  36. /// The local anchor point relative to bodyA's origin.
  37. b2Vec2 localAnchorA;
  38. /// The local anchor point relative to bodyB's origin.
  39. b2Vec2 localAnchorB;
  40. /// The maximum friction force in N.
  41. float maxForce;
  42. /// The maximum friction torque in N-m.
  43. float maxTorque;
  44. };
  45. /// Friction joint. This is used for top-down friction.
  46. /// It provides 2D translational friction and angular friction.
  47. class B2_API b2FrictionJoint : public b2Joint
  48. {
  49. public:
  50. b2Vec2 GetAnchorA() const override;
  51. b2Vec2 GetAnchorB() const override;
  52. b2Vec2 GetReactionForce(float inv_dt) const override;
  53. float GetReactionTorque(float inv_dt) const override;
  54. /// The local anchor point relative to bodyA's origin.
  55. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  56. /// The local anchor point relative to bodyB's origin.
  57. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  58. /// Set the maximum friction force in N.
  59. void SetMaxForce(float force);
  60. /// Get the maximum friction force in N.
  61. float GetMaxForce() const;
  62. /// Set the maximum friction torque in N*m.
  63. void SetMaxTorque(float torque);
  64. /// Get the maximum friction torque in N*m.
  65. float GetMaxTorque() const;
  66. /// Dump joint to dmLog
  67. void Dump() override;
  68. protected:
  69. friend class b2Joint;
  70. b2FrictionJoint(const b2FrictionJointDef* def);
  71. void InitVelocityConstraints(const b2SolverData& data) override;
  72. void SolveVelocityConstraints(const b2SolverData& data) override;
  73. bool SolvePositionConstraints(const b2SolverData& data) override;
  74. b2Vec2 m_localAnchorA;
  75. b2Vec2 m_localAnchorB;
  76. // Solver shared
  77. b2Vec2 m_linearImpulse;
  78. float m_angularImpulse;
  79. float m_maxForce;
  80. float m_maxTorque;
  81. // Solver temp
  82. int32 m_indexA;
  83. int32 m_indexB;
  84. b2Vec2 m_rA;
  85. b2Vec2 m_rB;
  86. b2Vec2 m_localCenterA;
  87. b2Vec2 m_localCenterB;
  88. float m_invMassA;
  89. float m_invMassB;
  90. float m_invIA;
  91. float m_invIB;
  92. b2Mat22 m_linearMass;
  93. float m_angularMass;
  94. };
  95. #endif