Box2D 2.4.1
A 2D physics engine for games
Loading...
Searching...
No Matches
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
29class b2Body;
30class b2Draw;
31class b2Joint;
32struct b2SolverData;
34
35enum 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_motorJoint
48};
49
50struct B2_API b2Jacobian
51{
52 b2Vec2 linear;
53 float angularA;
54 float angularB;
55};
56
69
71struct B2_API b2JointDef
72{
74 {
75 type = e_unknownJoint;
76 bodyA = nullptr;
77 bodyB = nullptr;
78 collideConnected = false;
79 }
80
82 b2JointType type;
83
86
89
92
95};
96
98B2_API void b2LinearStiffness(float& stiffness, float& damping,
99 float frequencyHertz, float dampingRatio,
100 const b2Body* bodyA, const b2Body* bodyB);
101
103B2_API void b2AngularStiffness(float& stiffness, float& damping,
104 float frequencyHertz, float dampingRatio,
105 const b2Body* bodyA, const b2Body* bodyB);
106
109class B2_API b2Joint
110{
111public:
112
114 b2JointType GetType() const;
115
117 b2Body* GetBodyA();
118
120 b2Body* GetBodyB();
121
123 virtual b2Vec2 GetAnchorA() const = 0;
124
126 virtual b2Vec2 GetAnchorB() const = 0;
127
129 virtual b2Vec2 GetReactionForce(float inv_dt) const = 0;
130
132 virtual float GetReactionTorque(float inv_dt) const = 0;
133
135 b2Joint* GetNext();
136 const b2Joint* GetNext() const;
137
139 b2JointUserData& GetUserData();
140 const b2JointUserData& GetUserData() const;
141
143 bool IsEnabled() const;
144
148 bool GetCollideConnected() const;
149
151 virtual void Dump() { b2Dump("// Dump is not supported for this joint type.\n"); }
152
154 virtual void ShiftOrigin(const b2Vec2& newOrigin) { B2_NOT_USED(newOrigin); }
155
157 virtual void Draw(b2Draw* draw) const;
158
159protected:
160 friend class b2World;
161 friend class b2Body;
162 friend class b2Island;
163 friend class b2GearJoint;
164
165 static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
166 static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
167
168 b2Joint(const b2JointDef* def);
169 virtual ~b2Joint() {}
170
171 virtual void InitVelocityConstraints(const b2SolverData& data) = 0;
172 virtual void SolveVelocityConstraints(const b2SolverData& data) = 0;
173
174 // This returns true if the position errors are within tolerance.
175 virtual bool SolvePositionConstraints(const b2SolverData& data) = 0;
176
177 b2JointType m_type;
178 b2Joint* m_prev;
179 b2Joint* m_next;
180 b2JointEdge m_edgeA;
181 b2JointEdge m_edgeB;
182 b2Body* m_bodyA;
183 b2Body* m_bodyB;
184
185 int32 m_index;
186
187 bool m_islandFlag;
188 bool m_collideConnected;
189
190 b2JointUserData m_userData;
191};
192
193inline b2JointType b2Joint::GetType() const
194{
195 return m_type;
196}
197
199{
200 return m_bodyA;
201}
202
204{
205 return m_bodyB;
206}
207
209{
210 return m_next;
211}
212
213inline const b2Joint* b2Joint::GetNext() const
214{
215 return m_next;
216}
217
219{
220 return m_userData;
221}
222
223inline const b2JointUserData& b2Joint::GetUserData() const
224{
225 return m_userData;
226}
227
229{
230 return m_collideConnected;
231}
232
233#endif
Definition b2_block_allocator.h:38
A rigid body. These are created via b2World::CreateBody.
Definition b2_body.h:129
Definition b2_draw.h:49
Definition b2_gear_joint.h:62
Definition b2_joint.h:110
b2Joint * GetNext()
Get the next joint the world joint list.
Definition b2_joint.h:208
b2Body * GetBodyA()
Get the first body attached to this joint.
Definition b2_joint.h:198
bool GetCollideConnected() const
Definition b2_joint.h:228
b2Body * GetBodyB()
Get the second body attached to this joint.
Definition b2_joint.h:203
virtual void ShiftOrigin(const b2Vec2 &newOrigin)
Shift the origin for any points stored in world coordinates.
Definition b2_joint.h:154
virtual b2Vec2 GetAnchorB() const =0
Get the anchor point on bodyB in world coordinates.
b2JointUserData & GetUserData()
Get the user data pointer.
Definition b2_joint.h:218
virtual void Dump()
Dump this joint to the log file.
Definition b2_joint.h:151
virtual b2Vec2 GetAnchorA() const =0
Get the anchor point on bodyA in world coordinates.
b2JointType GetType() const
Get the type of the concrete joint.
Definition b2_joint.h:193
bool IsEnabled() const
Short-cut function to determine if either body is enabled.
virtual b2Vec2 GetReactionForce(float inv_dt) const =0
Get the reaction force on bodyB at the joint anchor in Newtons.
virtual void Draw(b2Draw *draw) const
Debug draw this joint.
virtual float GetReactionTorque(float inv_dt) const =0
Get the reaction torque on bodyB in N*m.
Definition b2_world.h:47
Definition b2_joint.h:51
Joint definitions are used to construct joints.
Definition b2_joint.h:72
b2JointType type
The joint type is set automatically for concrete joint types.
Definition b2_joint.h:82
b2JointUserData userData
Use this to attach application specific data to your joints.
Definition b2_joint.h:85
b2Body * bodyA
The first attached body.
Definition b2_joint.h:88
b2Body * bodyB
The second attached body.
Definition b2_joint.h:91
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition b2_joint.h:94
Definition b2_joint.h:63
b2JointEdge * next
the next joint edge in the body's joint list
Definition b2_joint.h:67
b2Body * other
provides quick access to the other body attached.
Definition b2_joint.h:64
b2Joint * joint
the joint
Definition b2_joint.h:65
b2JointEdge * prev
the previous joint edge in the body's joint list
Definition b2_joint.h:66
You can define this to inject whatever data you want in b2Joint.
Definition b2_settings.h:83
Solver Data.
Definition b2_time_step.h:68
A 2D column vector.
Definition b2_math.h:42