b2_motor_joint.h 3.9 KB

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