b2GearJoint.h 3.4 KB

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