b2PrismaticJoint.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_PRISMATIC_JOINT_H
  19. #define B2_PRISMATIC_JOINT_H
  20. #include <Box2D/Dynamics/Joints/b2Joint.h>
  21. /// Prismatic joint definition. This requires defining a line of
  22. /// motion using an axis and an anchor point. The definition uses local
  23. /// anchor points and a local axis so that the initial configuration
  24. /// can violate the constraint slightly. The joint translation is zero
  25. /// when the local anchor points coincide in world space. Using local
  26. /// anchors and a local axis helps when saving and loading a game.
  27. struct b2PrismaticJointDef : public b2JointDef
  28. {
  29. b2PrismaticJointDef()
  30. {
  31. type = e_prismaticJoint;
  32. localAnchorA.SetZero();
  33. localAnchorB.SetZero();
  34. localAxisA.Set(1.0f, 0.0f);
  35. referenceAngle = 0.0f;
  36. enableLimit = false;
  37. lowerTranslation = 0.0f;
  38. upperTranslation = 0.0f;
  39. enableMotor = false;
  40. maxMotorForce = 0.0f;
  41. motorSpeed = 0.0f;
  42. }
  43. /// Initialize the bodies, anchors, axis, and reference angle using the world
  44. /// anchor and unit world axis.
  45. void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
  46. /// The local anchor point relative to bodyA's origin.
  47. b2Vec2 localAnchorA;
  48. /// The local anchor point relative to bodyB's origin.
  49. b2Vec2 localAnchorB;
  50. /// The local translation unit axis in bodyA.
  51. b2Vec2 localAxisA;
  52. /// The constrained angle between the bodies: bodyB_angle - bodyA_angle.
  53. float32 referenceAngle;
  54. /// Enable/disable the joint limit.
  55. bool enableLimit;
  56. /// The lower translation limit, usually in meters.
  57. float32 lowerTranslation;
  58. /// The upper translation limit, usually in meters.
  59. float32 upperTranslation;
  60. /// Enable/disable the joint motor.
  61. bool enableMotor;
  62. /// The maximum motor torque, usually in N-m.
  63. float32 maxMotorForce;
  64. /// The desired motor speed in radians per second.
  65. float32 motorSpeed;
  66. };
  67. /// A prismatic joint. This joint provides one degree of freedom: translation
  68. /// along an axis fixed in bodyA. Relative rotation is prevented. You can
  69. /// use a joint limit to restrict the range of motion and a joint motor to
  70. /// drive the motion or to model joint friction.
  71. class b2PrismaticJoint : public b2Joint
  72. {
  73. public:
  74. b2Vec2 GetAnchorA() const;
  75. b2Vec2 GetAnchorB() const;
  76. b2Vec2 GetReactionForce(float32 inv_dt) const;
  77. float32 GetReactionTorque(float32 inv_dt) const;
  78. /// The local anchor point relative to bodyA's origin.
  79. const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
  80. /// The local anchor point relative to bodyB's origin.
  81. const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
  82. /// The local joint axis relative to bodyA.
  83. const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
  84. /// Get the reference angle.
  85. float32 GetReferenceAngle() const { return m_referenceAngle; }
  86. /// Get the current joint translation, usually in meters.
  87. float32 GetJointTranslation() const;
  88. /// Get the current joint translation speed, usually in meters per second.
  89. float32 GetJointSpeed() const;
  90. /// Is the joint limit enabled?
  91. bool IsLimitEnabled() const;
  92. /// Enable/disable the joint limit.
  93. void EnableLimit(bool flag);
  94. /// Get the lower joint limit, usually in meters.
  95. float32 GetLowerLimit() const;
  96. /// Get the upper joint limit, usually in meters.
  97. float32 GetUpperLimit() const;
  98. /// Set the joint limits, usually in meters.
  99. void SetLimits(float32 lower, float32 upper);
  100. /// Is the joint motor enabled?
  101. bool IsMotorEnabled() const;
  102. /// Enable/disable the joint motor.
  103. void EnableMotor(bool flag);
  104. /// Set the motor speed, usually in meters per second.
  105. void SetMotorSpeed(float32 speed);
  106. /// Get the motor speed, usually in meters per second.
  107. float32 GetMotorSpeed() const;
  108. /// Set the maximum motor force, usually in N.
  109. void SetMaxMotorForce(float32 force);
  110. float32 GetMaxMotorForce() const { return m_maxMotorForce; }
  111. /// Get the current motor force given the inverse time step, usually in N.
  112. float32 GetMotorForce(float32 inv_dt) const;
  113. /// Dump to b2Log
  114. void Dump();
  115. protected:
  116. friend class b2Joint;
  117. friend class b2GearJoint;
  118. b2PrismaticJoint(const b2PrismaticJointDef* def);
  119. void InitVelocityConstraints(const b2SolverData& data);
  120. void SolveVelocityConstraints(const b2SolverData& data);
  121. bool SolvePositionConstraints(const b2SolverData& data);
  122. // Solver shared
  123. b2Vec2 m_localAnchorA;
  124. b2Vec2 m_localAnchorB;
  125. b2Vec2 m_localXAxisA;
  126. b2Vec2 m_localYAxisA;
  127. float32 m_referenceAngle;
  128. b2Vec3 m_impulse;
  129. float32 m_motorImpulse;
  130. float32 m_lowerTranslation;
  131. float32 m_upperTranslation;
  132. float32 m_maxMotorForce;
  133. float32 m_motorSpeed;
  134. bool m_enableLimit;
  135. bool m_enableMotor;
  136. b2LimitState m_limitState;
  137. // Solver temp
  138. int32 m_indexA;
  139. int32 m_indexB;
  140. b2Vec2 m_localCenterA;
  141. b2Vec2 m_localCenterB;
  142. float32 m_invMassA;
  143. float32 m_invMassB;
  144. float32 m_invIA;
  145. float32 m_invIB;
  146. b2Vec2 m_axis, m_perp;
  147. float32 m_s1, m_s2;
  148. float32 m_a1, m_a2;
  149. b2Mat33 m_K;
  150. float32 m_motorMass;
  151. };
  152. inline float32 b2PrismaticJoint::GetMotorSpeed() const
  153. {
  154. return m_motorSpeed;
  155. }
  156. #endif