b2_gear_joint.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_GEAR_JOINT_H
  19. #define B2_GEAR_JOINT_H
  20. #include "b2_joint.h"
  21. /// Gear joint definition. This definition requires two existing
  22. /// revolute or prismatic joints (any combination will work).
  23. /// @warning bodyB on the input joints must both be dynamic
  24. struct B2_API b2GearJointDef : public b2JointDef
  25. {
  26. b2GearJointDef()
  27. {
  28. type = e_gearJoint;
  29. joint1 = nullptr;
  30. joint2 = nullptr;
  31. ratio = 1.0f;
  32. }
  33. /// The first revolute/prismatic joint attached to the gear joint.
  34. b2Joint* joint1;
  35. /// The second revolute/prismatic joint attached to the gear joint.
  36. b2Joint* joint2;
  37. /// The gear ratio.
  38. /// @see b2GearJoint for explanation.
  39. float ratio;
  40. };
  41. /// A gear joint is used to connect two joints together. Either joint
  42. /// can be a revolute or prismatic joint. You specify a gear ratio
  43. /// to bind the motions together:
  44. /// coordinate1 + ratio * coordinate2 = constant
  45. /// The ratio can be negative or positive. If one joint is a revolute joint
  46. /// and the other joint is a prismatic joint, then the ratio will have units
  47. /// of length or units of 1/length.
  48. /// @warning You have to manually destroy the gear joint if joint1 or joint2
  49. /// is destroyed.
  50. class B2_API b2GearJoint : 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. /// Get the first joint.
  58. b2Joint* GetJoint1() { return m_joint1; }
  59. /// Get the second joint.
  60. b2Joint* GetJoint2() { return m_joint2; }
  61. /// Set/Get the gear ratio.
  62. void SetRatio(float ratio);
  63. float GetRatio() const;
  64. /// Dump joint to dmLog
  65. void Dump() override;
  66. protected:
  67. friend class b2Joint;
  68. b2GearJoint(const b2GearJointDef* data);
  69. void InitVelocityConstraints(const b2SolverData& data) override;
  70. void SolveVelocityConstraints(const b2SolverData& data) override;
  71. bool SolvePositionConstraints(const b2SolverData& data) override;
  72. b2Joint* m_joint1;
  73. b2Joint* m_joint2;
  74. b2JointType m_typeA;
  75. b2JointType m_typeB;
  76. // Body A is connected to body C
  77. // Body B is connected to body D
  78. b2Body* m_bodyC;
  79. b2Body* m_bodyD;
  80. // Solver shared
  81. b2Vec2 m_localAnchorA;
  82. b2Vec2 m_localAnchorB;
  83. b2Vec2 m_localAnchorC;
  84. b2Vec2 m_localAnchorD;
  85. b2Vec2 m_localAxisC;
  86. b2Vec2 m_localAxisD;
  87. float m_referenceAngleA;
  88. float m_referenceAngleB;
  89. float m_constant;
  90. float m_ratio;
  91. float m_tolerance;
  92. float m_impulse;
  93. // Solver temp
  94. int32 m_indexA, m_indexB, m_indexC, m_indexD;
  95. b2Vec2 m_lcA, m_lcB, m_lcC, m_lcD;
  96. float m_mA, m_mB, m_mC, m_mD;
  97. float m_iA, m_iB, m_iC, m_iD;
  98. b2Vec2 m_JvAC, m_JvBD;
  99. float m_JwA, m_JwB, m_JwC, m_JwD;
  100. float m_mass;
  101. };
  102. #endif