b2FrictionJoint.h 3.2 KB

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