b2_joint.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_JOINT_H
  19. #define B2_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_math.h"
  22. class b2Body;
  23. class b2Draw;
  24. class b2Joint;
  25. struct b2SolverData;
  26. class b2BlockAllocator;
  27. enum b2JointType
  28. {
  29. e_unknownJoint,
  30. e_revoluteJoint,
  31. e_prismaticJoint,
  32. e_distanceJoint,
  33. e_pulleyJoint,
  34. e_mouseJoint,
  35. e_gearJoint,
  36. e_wheelJoint,
  37. e_weldJoint,
  38. e_frictionJoint,
  39. e_motorJoint
  40. };
  41. struct B2_API b2Jacobian
  42. {
  43. b2Vec2 linear;
  44. float angularA;
  45. float angularB;
  46. };
  47. /// A joint edge is used to connect bodies and joints together
  48. /// in a joint graph where each body is a node and each joint
  49. /// is an edge. A joint edge belongs to a doubly linked list
  50. /// maintained in each attached body. Each joint has two joint
  51. /// nodes, one for each attached body.
  52. struct B2_API b2JointEdge
  53. {
  54. b2Body* other; ///< provides quick access to the other body attached.
  55. b2Joint* joint; ///< the joint
  56. b2JointEdge* prev; ///< the previous joint edge in the body's joint list
  57. b2JointEdge* next; ///< the next joint edge in the body's joint list
  58. };
  59. /// Joint definitions are used to construct joints.
  60. struct B2_API b2JointDef
  61. {
  62. b2JointDef()
  63. {
  64. type = e_unknownJoint;
  65. bodyA = nullptr;
  66. bodyB = nullptr;
  67. collideConnected = false;
  68. }
  69. /// The joint type is set automatically for concrete joint types.
  70. b2JointType type;
  71. /// Use this to attach application specific data to your joints.
  72. b2JointUserData userData;
  73. /// The first attached body.
  74. b2Body* bodyA;
  75. /// The second attached body.
  76. b2Body* bodyB;
  77. /// Set this flag to true if the attached bodies should collide.
  78. bool collideConnected;
  79. };
  80. /// Utility to compute linear stiffness values from frequency and damping ratio
  81. B2_API void b2LinearStiffness(float& stiffness, float& damping,
  82. float frequencyHertz, float dampingRatio,
  83. const b2Body* bodyA, const b2Body* bodyB);
  84. /// Utility to compute rotational stiffness values frequency and damping ratio
  85. B2_API void b2AngularStiffness(float& stiffness, float& damping,
  86. float frequencyHertz, float dampingRatio,
  87. const b2Body* bodyA, const b2Body* bodyB);
  88. /// The base joint class. Joints are used to constraint two bodies together in
  89. /// various fashions. Some joints also feature limits and motors.
  90. class B2_API b2Joint
  91. {
  92. public:
  93. /// Get the type of the concrete joint.
  94. b2JointType GetType() const;
  95. /// Get the first body attached to this joint.
  96. b2Body* GetBodyA();
  97. /// Get the second body attached to this joint.
  98. b2Body* GetBodyB();
  99. /// Get the anchor point on bodyA in world coordinates.
  100. virtual b2Vec2 GetAnchorA() const = 0;
  101. /// Get the anchor point on bodyB in world coordinates.
  102. virtual b2Vec2 GetAnchorB() const = 0;
  103. /// Get the reaction force on bodyB at the joint anchor in Newtons.
  104. virtual b2Vec2 GetReactionForce(float inv_dt) const = 0;
  105. /// Get the reaction torque on bodyB in N*m.
  106. virtual float GetReactionTorque(float inv_dt) const = 0;
  107. /// Get the next joint the world joint list.
  108. b2Joint* GetNext();
  109. const b2Joint* GetNext() const;
  110. /// Get the user data pointer.
  111. b2JointUserData& GetUserData();
  112. const b2JointUserData& GetUserData() const;
  113. /// Short-cut function to determine if either body is enabled.
  114. bool IsEnabled() const;
  115. /// Get collide connected.
  116. /// Note: modifying the collide connect flag won't work correctly because
  117. /// the flag is only checked when fixture AABBs begin to overlap.
  118. bool GetCollideConnected() const;
  119. /// Dump this joint to the log file.
  120. virtual void Dump() { b2Dump("// Dump is not supported for this joint type.\n"); }
  121. /// Shift the origin for any points stored in world coordinates.
  122. virtual void ShiftOrigin(const b2Vec2& newOrigin) { B2_NOT_USED(newOrigin); }
  123. /// Debug draw this joint
  124. virtual void Draw(b2Draw* draw) const;
  125. protected:
  126. friend class b2World;
  127. friend class b2Body;
  128. friend class b2Island;
  129. friend class b2GearJoint;
  130. static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
  131. static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
  132. b2Joint(const b2JointDef* def);
  133. virtual ~b2Joint() {}
  134. virtual void InitVelocityConstraints(const b2SolverData& data) = 0;
  135. virtual void SolveVelocityConstraints(const b2SolverData& data) = 0;
  136. // This returns true if the position errors are within tolerance.
  137. virtual bool SolvePositionConstraints(const b2SolverData& data) = 0;
  138. b2JointType m_type;
  139. b2Joint* m_prev;
  140. b2Joint* m_next;
  141. b2JointEdge m_edgeA;
  142. b2JointEdge m_edgeB;
  143. b2Body* m_bodyA;
  144. b2Body* m_bodyB;
  145. int32 m_index;
  146. bool m_islandFlag;
  147. bool m_collideConnected;
  148. b2JointUserData m_userData;
  149. };
  150. inline b2JointType b2Joint::GetType() const
  151. {
  152. return m_type;
  153. }
  154. inline b2Body* b2Joint::GetBodyA()
  155. {
  156. return m_bodyA;
  157. }
  158. inline b2Body* b2Joint::GetBodyB()
  159. {
  160. return m_bodyB;
  161. }
  162. inline b2Joint* b2Joint::GetNext()
  163. {
  164. return m_next;
  165. }
  166. inline const b2Joint* b2Joint::GetNext() const
  167. {
  168. return m_next;
  169. }
  170. inline b2JointUserData& b2Joint::GetUserData()
  171. {
  172. return m_userData;
  173. }
  174. inline const b2JointUserData& b2Joint::GetUserData() const
  175. {
  176. return m_userData;
  177. }
  178. inline bool b2Joint::GetCollideConnected() const
  179. {
  180. return m_collideConnected;
  181. }
  182. #endif