Box2D  2.4.0
A 2D physics engine for games
b2_joint.h
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef B2_JOINT_H
24 #define B2_JOINT_H
25 
26 #include "b2_api.h"
27 #include "b2_math.h"
28 
29 class b2Body;
30 class b2Draw;
31 class b2Joint;
32 struct b2SolverData;
33 class b2BlockAllocator;
34 
35 enum b2JointType
36 {
37  e_unknownJoint,
38  e_revoluteJoint,
39  e_prismaticJoint,
40  e_distanceJoint,
41  e_pulleyJoint,
42  e_mouseJoint,
43  e_gearJoint,
44  e_wheelJoint,
45  e_weldJoint,
46  e_frictionJoint,
47  e_ropeJoint,
48  e_motorJoint
49 };
50 
51 struct B2_API b2Jacobian
52 {
53  b2Vec2 linear;
54  float angularA;
55  float angularB;
56 };
57 
63 struct B2_API b2JointEdge
64 {
69 };
70 
72 struct B2_API b2JointDef
73 {
74  b2JointDef()
75  {
76  type = e_unknownJoint;
77  userData = nullptr;
78  bodyA = nullptr;
79  bodyB = nullptr;
80  collideConnected = false;
81  }
82 
84  b2JointType type;
85 
87  void* userData;
88 
91 
94 
97 };
98 
100 B2_API void b2LinearStiffness(float& stiffness, float& damping,
101  float frequencyHertz, float dampingRatio,
102  const b2Body* bodyA, const b2Body* bodyB);
103 
105 B2_API void b2AngularStiffness(float& stiffness, float& damping,
106  float frequencyHertz, float dampingRatio,
107  const b2Body* bodyA, const b2Body* bodyB);
108 
111 class B2_API b2Joint
112 {
113 public:
114 
116  b2JointType GetType() const;
117 
119  b2Body* GetBodyA();
120 
122  b2Body* GetBodyB();
123 
125  virtual b2Vec2 GetAnchorA() const = 0;
126 
128  virtual b2Vec2 GetAnchorB() const = 0;
129 
131  virtual b2Vec2 GetReactionForce(float inv_dt) const = 0;
132 
134  virtual float GetReactionTorque(float inv_dt) const = 0;
135 
137  b2Joint* GetNext();
138  const b2Joint* GetNext() const;
139 
141  void* GetUserData() const;
142 
144  void SetUserData(void* data);
145 
147  bool IsEnabled() const;
148 
152  bool GetCollideConnected() const;
153 
155  virtual void Dump() { b2Dump("// Dump is not supported for this joint type.\n"); }
156 
158  virtual void ShiftOrigin(const b2Vec2& newOrigin) { B2_NOT_USED(newOrigin); }
159 
161  virtual void Draw(b2Draw* draw) const;
162 
163 protected:
164  friend class b2World;
165  friend class b2Body;
166  friend class b2Island;
167  friend class b2GearJoint;
168 
169  static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
170  static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
171 
172  b2Joint(const b2JointDef* def);
173  virtual ~b2Joint() {}
174 
175  virtual void InitVelocityConstraints(const b2SolverData& data) = 0;
176  virtual void SolveVelocityConstraints(const b2SolverData& data) = 0;
177 
178  // This returns true if the position errors are within tolerance.
179  virtual bool SolvePositionConstraints(const b2SolverData& data) = 0;
180 
181  b2JointType m_type;
182  b2Joint* m_prev;
183  b2Joint* m_next;
184  b2JointEdge m_edgeA;
185  b2JointEdge m_edgeB;
186  b2Body* m_bodyA;
187  b2Body* m_bodyB;
188 
189  int32 m_index;
190 
191  bool m_islandFlag;
192  bool m_collideConnected;
193 
194  void* m_userData;
195 };
196 
197 inline b2JointType b2Joint::GetType() const
198 {
199  return m_type;
200 }
201 
203 {
204  return m_bodyA;
205 }
206 
208 {
209  return m_bodyB;
210 }
211 
213 {
214  return m_next;
215 }
216 
217 inline const b2Joint* b2Joint::GetNext() const
218 {
219  return m_next;
220 }
221 
222 inline void* b2Joint::GetUserData() const
223 {
224  return m_userData;
225 }
226 
227 inline void b2Joint::SetUserData(void* data)
228 {
229  m_userData = data;
230 }
231 
232 inline bool b2Joint::GetCollideConnected() const
233 {
234  return m_collideConnected;
235 }
236 
237 #endif
b2Vec2
A 2D column vector.
Definition: b2_math.h:42
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:133
b2Joint::GetUserData
void * GetUserData() const
Get the user data pointer.
Definition: b2_joint.h:222
b2Joint::GetBodyA
b2Body * GetBodyA()
Get the first body attached to this joint.
Definition: b2_joint.h:202
b2Joint::GetReactionForce
virtual b2Vec2 GetReactionForce(float inv_dt) const =0
Get the reaction force on bodyB at the joint anchor in Newtons.
b2JointDef::collideConnected
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition: b2_joint.h:96
b2Joint::GetAnchorA
virtual b2Vec2 GetAnchorA() const =0
Get the anchor point on bodyA in world coordinates.
b2Joint::GetBodyB
b2Body * GetBodyB()
Get the second body attached to this joint.
Definition: b2_joint.h:207
b2Draw
Definition: b2_draw.h:49
b2JointEdge::joint
b2Joint * joint
the joint
Definition: b2_joint.h:66
b2JointDef
Joint definitions are used to construct joints.
Definition: b2_joint.h:73
b2JointDef::type
b2JointType type
The joint type is set automatically for concrete joint types.
Definition: b2_joint.h:84
b2Joint::SetUserData
void SetUserData(void *data)
Set the user data pointer.
Definition: b2_joint.h:227
b2Joint::Draw
virtual void Draw(b2Draw *draw) const
Debug draw this joint.
b2Joint::GetType
b2JointType GetType() const
Get the type of the concrete joint.
Definition: b2_joint.h:197
b2Joint::IsEnabled
bool IsEnabled() const
Short-cut function to determine if either body is enabled.
b2Joint::GetCollideConnected
bool GetCollideConnected() const
Definition: b2_joint.h:232
b2JointDef::bodyB
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:93
b2Joint::Dump
virtual void Dump()
Dump this joint to the log file.
Definition: b2_joint.h:155
b2BlockAllocator
Definition: b2_block_allocator.h:38
b2Jacobian
Definition: b2_joint.h:52
b2Joint::ShiftOrigin
virtual void ShiftOrigin(const b2Vec2 &newOrigin)
Shift the origin for any points stored in world coordinates.
Definition: b2_joint.h:158
b2JointEdge
Definition: b2_joint.h:64
b2JointEdge::prev
b2JointEdge * prev
the previous joint edge in the body's joint list
Definition: b2_joint.h:67
b2World
Definition: b2_world.h:47
b2GearJoint
Definition: b2_gear_joint.h:61
b2SolverData
Solver Data.
Definition: b2_time_step.h:68
b2JointEdge::next
b2JointEdge * next
the next joint edge in the body's joint list
Definition: b2_joint.h:68
b2Joint::GetAnchorB
virtual b2Vec2 GetAnchorB() const =0
Get the anchor point on bodyB in world coordinates.
b2JointEdge::other
b2Body * other
provides quick access to the other body attached.
Definition: b2_joint.h:65
b2JointDef::bodyA
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:90
b2Joint
Definition: b2_joint.h:112
b2Joint::GetReactionTorque
virtual float GetReactionTorque(float inv_dt) const =0
Get the reaction torque on bodyB in N*m.
b2JointDef::userData
void * userData
Use this to attach application specific data to your joints.
Definition: b2_joint.h:87
b2Joint::GetNext
b2Joint * GetNext()
Get the next joint the world joint list.
Definition: b2_joint.h:212