Box2D  2.4.1
A 2D physics engine for games
b2_world.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_WORLD_H
24 #define B2_WORLD_H
25 
26 #include "b2_api.h"
27 #include "b2_block_allocator.h"
28 #include "b2_contact_manager.h"
29 #include "b2_math.h"
30 #include "b2_stack_allocator.h"
31 #include "b2_time_step.h"
32 #include "b2_world_callbacks.h"
33 
34 struct b2AABB;
35 struct b2BodyDef;
36 struct b2Color;
37 struct b2JointDef;
38 class b2Body;
39 class b2Draw;
40 class b2Fixture;
41 class b2Joint;
42 
46 class B2_API b2World
47 {
48 public:
51  b2World(const b2Vec2& gravity);
52 
55 
59 
64 
68 
72  void SetDebugDraw(b2Draw* debugDraw);
73 
77  b2Body* CreateBody(const b2BodyDef* def);
78 
83  void DestroyBody(b2Body* body);
84 
89 
92  void DestroyJoint(b2Joint* joint);
93 
99  void Step( float timeStep,
100  int32 velocityIterations,
101  int32 positionIterations);
102 
110  void ClearForces();
111 
113  void DebugDraw();
114 
119  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
120 
127  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
128 
132  b2Body* GetBodyList();
133  const b2Body* GetBodyList() const;
134 
138  b2Joint* GetJointList();
139  const b2Joint* GetJointList() const;
140 
146  b2Contact* GetContactList();
147  const b2Contact* GetContactList() const;
148 
150  void SetAllowSleeping(bool flag);
151  bool GetAllowSleeping() const { return m_allowSleep; }
152 
154  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
155  bool GetWarmStarting() const { return m_warmStarting; }
156 
158  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
159  bool GetContinuousPhysics() const { return m_continuousPhysics; }
160 
162  void SetSubStepping(bool flag) { m_subStepping = flag; }
163  bool GetSubStepping() const { return m_subStepping; }
164 
166  int32 GetProxyCount() const;
167 
169  int32 GetBodyCount() const;
170 
172  int32 GetJointCount() const;
173 
175  int32 GetContactCount() const;
176 
178  int32 GetTreeHeight() const;
179 
181  int32 GetTreeBalance() const;
182 
185  float GetTreeQuality() const;
186 
188  void SetGravity(const b2Vec2& gravity);
189 
191  b2Vec2 GetGravity() const;
192 
194  bool IsLocked() const;
195 
197  void SetAutoClearForces(bool flag);
198 
200  bool GetAutoClearForces() const;
201 
205  void ShiftOrigin(const b2Vec2& newOrigin);
206 
208  const b2ContactManager& GetContactManager() const;
209 
211  const b2Profile& GetProfile() const;
212 
215  void Dump();
216 
217 private:
218 
219  friend class b2Body;
220  friend class b2Fixture;
221  friend class b2ContactManager;
222  friend class b2Controller;
223 
224  void Solve(const b2TimeStep& step);
225  void SolveTOI(const b2TimeStep& step);
226 
227  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
228 
229  b2BlockAllocator m_blockAllocator;
230  b2StackAllocator m_stackAllocator;
231 
232  b2ContactManager m_contactManager;
233 
234  b2Body* m_bodyList;
235  b2Joint* m_jointList;
236 
237  int32 m_bodyCount;
238  int32 m_jointCount;
239 
240  b2Vec2 m_gravity;
241  bool m_allowSleep;
242 
243  b2DestructionListener* m_destructionListener;
244  b2Draw* m_debugDraw;
245 
246  // This is used to compute the time step ratio to
247  // support a variable time step.
248  float m_inv_dt0;
249 
250  bool m_newContacts;
251  bool m_locked;
252  bool m_clearForces;
253 
254  // These are for debugging the solver.
255  bool m_warmStarting;
256  bool m_continuousPhysics;
257  bool m_subStepping;
258 
259  bool m_stepComplete;
260 
261  b2Profile m_profile;
262 };
263 
265 {
266  return m_bodyList;
267 }
268 
269 inline const b2Body* b2World::GetBodyList() const
270 {
271  return m_bodyList;
272 }
273 
275 {
276  return m_jointList;
277 }
278 
279 inline const b2Joint* b2World::GetJointList() const
280 {
281  return m_jointList;
282 }
283 
285 {
286  return m_contactManager.m_contactList;
287 }
288 
289 inline const b2Contact* b2World::GetContactList() const
290 {
291  return m_contactManager.m_contactList;
292 }
293 
294 inline int32 b2World::GetBodyCount() const
295 {
296  return m_bodyCount;
297 }
298 
299 inline int32 b2World::GetJointCount() const
300 {
301  return m_jointCount;
302 }
303 
304 inline int32 b2World::GetContactCount() const
305 {
306  return m_contactManager.m_contactCount;
307 }
308 
309 inline void b2World::SetGravity(const b2Vec2& gravity)
310 {
311  m_gravity = gravity;
312 }
313 
315 {
316  return m_gravity;
317 }
318 
319 inline bool b2World::IsLocked() const
320 {
321  return m_locked;
322 }
323 
324 inline void b2World::SetAutoClearForces(bool flag)
325 {
326  m_clearForces = flag;
327 }
328 
330 inline bool b2World::GetAutoClearForces() const
331 {
332  return m_clearForces;
333 }
334 
336 {
337  return m_contactManager;
338 }
339 
340 inline const b2Profile& b2World::GetProfile() const
341 {
342  return m_profile;
343 }
344 
345 #endif
Definition: b2_block_allocator.h:38
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:129
Definition: b2_world_callbacks.h:58
Definition: b2_contact.h:89
Definition: b2_world_callbacks.h:87
Definition: b2_contact_manager.h:36
Definition: b2_world_callbacks.h:42
Definition: b2_draw.h:49
Definition: b2_fixture.h:117
Definition: b2_joint.h:111
Definition: b2_world_callbacks.h:129
Definition: b2_world_callbacks.h:141
Definition: b2_stack_allocator.h:43
Definition: b2_world.h:47
int32 GetProxyCount() const
Get the number of broad-phase proxies.
b2Body * GetBodyList()
Definition: b2_world.h:264
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2_world.h:335
int32 GetBodyCount() const
Get the number of bodies.
Definition: b2_world.h:294
~b2World()
Destruct the world. All physics entities are destroyed and all heap memory is released.
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2_world.h:158
b2Joint * GetJointList()
Definition: b2_world.h:274
void SetContactListener(b2ContactListener *listener)
void SetAllowSleeping(bool flag)
Enable/disable sleep.
void SetDebugDraw(b2Draw *debugDraw)
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2_world.h:319
void Dump()
void Step(float timeStep, int32 velocityIterations, int32 positionIterations)
void SetContactFilter(b2ContactFilter *filter)
float GetTreeQuality() const
b2Joint * CreateJoint(const b2JointDef *def)
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2_world.h:154
b2Body * CreateBody(const b2BodyDef *def)
int32 GetJointCount() const
Get the number of joints.
Definition: b2_world.h:299
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2_world.h:324
int32 GetContactCount() const
Get the number of contacts (each may have 0 or more contact points).
Definition: b2_world.h:304
void DebugDraw()
Call this to draw shapes and other debug draw data. This is intentionally non-const.
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
b2Contact * GetContactList()
Definition: b2_world.h:284
int32 GetTreeHeight() const
Get the height of the dynamic tree.
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2_world.h:314
void ClearForces()
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
void DestroyBody(b2Body *body)
void DestroyJoint(b2Joint *joint)
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2_world.h:330
void SetDestructionListener(b2DestructionListener *listener)
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2_world.h:162
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2_world.h:309
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2_world.h:340
b2World(const b2Vec2 &gravity)
void ShiftOrigin(const b2Vec2 &newOrigin)
An axis aligned bounding box.
Definition: b2_collision.h:169
Definition: b2_body.h:53
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:31
Joint definitions are used to construct joints.
Definition: b2_joint.h:73
Profiling data. Times are in milliseconds.
Definition: b2_time_step.h:30
This is an internal structure.
Definition: b2_time_step.h:43
Definition: b2_math.h:339
A 2D column vector.
Definition: b2_math.h:42