b2MotorJoint.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2012 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_MOTOR_JOINT_H
  19. #define B2_MOTOR_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Motor joint definition.
  22. struct b2MotorJointDef : public b2JointDef
  23. {
  24. b2MotorJointDef()
  25. {
  26. type = e_motorJoint;
  27. linearOffset.SetZero();
  28. angularOffset = 0.0f;
  29. maxForce = 1.0f;
  30. maxTorque = 1.0f;
  31. correctionFactor = 0.3f;
  32. }
  33. /// Initialize the bodies and offsets using the current transforms.
  34. void Initialize(b2Body* bodyA, b2Body* bodyB);
  35. /// Position of bodyB minus the position of bodyA, in bodyA's frame, in meters.
  36. b2Vec2 linearOffset;
  37. /// The bodyB angle minus bodyA angle in radians.
  38. float32 angularOffset;
  39. /// The maximum motor force in N.
  40. float32 maxForce;
  41. /// The maximum motor torque in N-m.
  42. float32 maxTorque;
  43. /// Position correction factor in the range [0,1].
  44. float32 correctionFactor;
  45. };
  46. /// A motor joint is used to control the relative motion
  47. /// between two bodies. A typical usage is to control the movement
  48. /// of a dynamic body with respect to the ground.
  49. class b2MotorJoint : public b2Joint
  50. {
  51. public:
  52. b2Vec2 GetAnchorA() const;
  53. b2Vec2 GetAnchorB() const;
  54. b2Vec2 GetReactionForce(float32 inv_dt) const;
  55. float32 GetReactionTorque(float32 inv_dt) const;
  56. /// Set/get the target linear offset, in frame A, in meters.
  57. void SetLinearOffset(const b2Vec2& linearOffset);
  58. const b2Vec2& GetLinearOffset() const;
  59. /// Set/get the target angular offset, in radians.
  60. void SetAngularOffset(float32 angularOffset);
  61. float32 GetAngularOffset() const;
  62. /// Set the maximum friction force in N.
  63. void SetMaxForce(float32 force);
  64. /// Get the maximum friction force in N.
  65. float32 GetMaxForce() const;
  66. /// Set the maximum friction torque in N*m.
  67. void SetMaxTorque(float32 torque);
  68. /// Get the maximum friction torque in N*m.
  69. float32 GetMaxTorque() const;
  70. /// Set the position correction factor in the range [0,1].
  71. void SetCorrectionFactor(float32 factor);
  72. /// Get the position correction factor in the range [0,1].
  73. float32 GetCorrectionFactor() const;
  74. /// Dump to b2Log
  75. void Dump();
  76. protected:
  77. friend class b2Joint;
  78. b2MotorJoint(const b2MotorJointDef* def);
  79. void InitVelocityConstraints(const b2SolverData& data);
  80. void SolveVelocityConstraints(const b2SolverData& data);
  81. bool SolvePositionConstraints(const b2SolverData& data);
  82. // Solver shared
  83. b2Vec2 m_linearOffset;
  84. float32 m_angularOffset;
  85. b2Vec2 m_linearImpulse;
  86. float32 m_angularImpulse;
  87. float32 m_maxForce;
  88. float32 m_maxTorque;
  89. float32 m_correctionFactor;
  90. // Solver temp
  91. int32 m_indexA;
  92. int32 m_indexB;
  93. b2Vec2 m_rA;
  94. b2Vec2 m_rB;
  95. b2Vec2 m_localCenterA;
  96. b2Vec2 m_localCenterB;
  97. b2Vec2 m_linearError;
  98. float32 m_angularError;
  99. float32 m_invMassA;
  100. float32 m_invMassB;
  101. float32 m_invIA;
  102. float32 m_invIB;
  103. b2Mat22 m_linearMass;
  104. float32 m_angularMass;
  105. };
  106. #endif