The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.4KB

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