b2Joint.h 5.4 KB

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