Box2D  2.4.0
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_block_allocator.h"
27 #include "b2_contact_manager.h"
28 #include "b2_math.h"
29 #include "b2_stack_allocator.h"
30 #include "b2_time_step.h"
31 #include "b2_world_callbacks.h"
32 
33 struct b2AABB;
34 struct b2BodyDef;
35 struct b2Color;
36 struct b2JointDef;
37 class b2Body;
38 class b2Draw;
39 class b2Fixture;
40 class b2Joint;
41 
45 class b2World
46 {
47 public:
50  b2World(const b2Vec2& gravity);
51 
53  ~b2World();
54 
58 
62  void SetContactFilter(b2ContactFilter* filter);
63 
66  void SetContactListener(b2ContactListener* listener);
67 
71  void SetDebugDraw(b2Draw* debugDraw);
72 
76  b2Body* CreateBody(const b2BodyDef* def);
77 
82  void DestroyBody(b2Body* body);
83 
87  b2Joint* CreateJoint(const b2JointDef* def);
88 
91  void DestroyJoint(b2Joint* joint);
92 
98  void Step( float timeStep,
99  int32 velocityIterations,
100  int32 positionIterations);
101 
109  void ClearForces();
110 
112  void DrawDebugData();
113 
118  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
119 
126  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
127 
131  b2Body* GetBodyList();
132  const b2Body* GetBodyList() const;
133 
138  const b2Joint* GetJointList() const;
139 
146  const b2Contact* GetContactList() const;
147 
149  void SetAllowSleeping(bool flag);
150  bool GetAllowSleeping() const { return m_allowSleep; }
151 
153  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
154  bool GetWarmStarting() const { return m_warmStarting; }
155 
157  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
158  bool GetContinuousPhysics() const { return m_continuousPhysics; }
159 
161  void SetSubStepping(bool flag) { m_subStepping = flag; }
162  bool GetSubStepping() const { return m_subStepping; }
163 
165  int32 GetProxyCount() const;
166 
168  int32 GetBodyCount() const;
169 
171  int32 GetJointCount() const;
172 
174  int32 GetContactCount() const;
175 
177  int32 GetTreeHeight() const;
178 
180  int32 GetTreeBalance() const;
181 
184  float GetTreeQuality() const;
185 
187  void SetGravity(const b2Vec2& gravity);
188 
190  b2Vec2 GetGravity() const;
191 
193  bool IsLocked() const;
194 
196  void SetAutoClearForces(bool flag);
197 
199  bool GetAutoClearForces() const;
200 
204  void ShiftOrigin(const b2Vec2& newOrigin);
205 
207  const b2ContactManager& GetContactManager() const;
208 
210  const b2Profile& GetProfile() const;
211 
214  void Dump();
215 
216 private:
217 
218  // m_flags
219  enum
220  {
221  e_newFixture = 0x0001,
222  e_locked = 0x0002,
223  e_clearForces = 0x0004
224  };
225 
226  friend class b2Body;
227  friend class b2Fixture;
228  friend class b2ContactManager;
229  friend class b2Controller;
230 
231  void Solve(const b2TimeStep& step);
232  void SolveTOI(const b2TimeStep& step);
233 
234  void DrawJoint(b2Joint* joint);
235  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
236 
237  b2BlockAllocator m_blockAllocator;
238  b2StackAllocator m_stackAllocator;
239 
240  int32 m_flags;
241 
242  b2ContactManager m_contactManager;
243 
244  b2Body* m_bodyList;
245  b2Joint* m_jointList;
246 
247  int32 m_bodyCount;
248  int32 m_jointCount;
249 
250  b2Vec2 m_gravity;
251  bool m_allowSleep;
252 
253  b2DestructionListener* m_destructionListener;
254  b2Draw* m_debugDraw;
255 
256  // This is used to compute the time step ratio to
257  // support a variable time step.
258  float m_inv_dt0;
259 
260  // These are for debugging the solver.
261  bool m_warmStarting;
262  bool m_continuousPhysics;
263  bool m_subStepping;
264 
265  bool m_stepComplete;
266 
267  b2Profile m_profile;
268 };
269 
271 {
272  return m_bodyList;
273 }
274 
275 inline const b2Body* b2World::GetBodyList() const
276 {
277  return m_bodyList;
278 }
279 
281 {
282  return m_jointList;
283 }
284 
285 inline const b2Joint* b2World::GetJointList() const
286 {
287  return m_jointList;
288 }
289 
291 {
292  return m_contactManager.m_contactList;
293 }
294 
295 inline const b2Contact* b2World::GetContactList() const
296 {
297  return m_contactManager.m_contactList;
298 }
299 
300 inline int32 b2World::GetBodyCount() const
301 {
302  return m_bodyCount;
303 }
304 
305 inline int32 b2World::GetJointCount() const
306 {
307  return m_jointCount;
308 }
309 
310 inline int32 b2World::GetContactCount() const
311 {
312  return m_contactManager.m_contactCount;
313 }
314 
315 inline void b2World::SetGravity(const b2Vec2& gravity)
316 {
317  m_gravity = gravity;
318 }
319 
321 {
322  return m_gravity;
323 }
324 
325 inline bool b2World::IsLocked() const
326 {
327  return (m_flags & e_locked) == e_locked;
328 }
329 
330 inline void b2World::SetAutoClearForces(bool flag)
331 {
332  if (flag)
333  {
334  m_flags |= e_clearForces;
335  }
336  else
337  {
338  m_flags &= ~e_clearForces;
339  }
340 }
341 
343 inline bool b2World::GetAutoClearForces() const
344 {
345  return (m_flags & e_clearForces) == e_clearForces;
346 }
347 
349 {
350  return m_contactManager;
351 }
352 
353 inline const b2Profile& b2World::GetProfile() const
354 {
355  return m_profile;
356 }
357 
358 #endif
b2World::ClearForces
void ClearForces()
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
b2Vec2
A 2D column vector.
Definition: b2_math.h:39
b2TimeStep
This is an internal structure.
Definition: b2_time_step.h:41
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:131
b2ContactManager
Definition: b2_contact_manager.h:34
b2World::SetDestructionListener
void SetDestructionListener(b2DestructionListener *listener)
b2World::SetContactListener
void SetContactListener(b2ContactListener *listener)
b2World::GetContactCount
int32 GetContactCount() const
Get the number of contacts (each may have 0 or more contact points).
Definition: b2_world.h:310
b2World::GetBodyList
b2Body * GetBodyList()
Definition: b2_world.h:270
b2Profile
Profiling data. Times are in milliseconds.
Definition: b2_time_step.h:28
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
b2World::GetContactManager
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2_world.h:348
b2ContactListener
Definition: b2_world_callbacks.h:85
b2Draw
Definition: b2_draw.h:47
b2World::DestroyBody
void DestroyBody(b2Body *body)
b2Transform
Definition: b2_math.h:336
b2World::SetWarmStarting
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2_world.h:153
b2JointDef
Joint definitions are used to construct joints.
Definition: b2_joint.h:78
b2QueryCallback
Definition: b2_world_callbacks.h:127
b2World::GetBodyCount
int32 GetBodyCount() const
Get the number of bodies.
Definition: b2_world.h:300
b2World::SetGravity
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2_world.h:315
b2World::GetTreeHeight
int32 GetTreeHeight() const
Get the height of the dynamic tree.
b2StackAllocator
Definition: b2_stack_allocator.h:41
b2World::GetTreeQuality
float GetTreeQuality() const
b2World::GetJointList
b2Joint * GetJointList()
Definition: b2_world.h:280
b2World::DestroyJoint
void DestroyJoint(b2Joint *joint)
b2World::GetJointCount
int32 GetJointCount() const
Get the number of joints.
Definition: b2_world.h:305
b2Color
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:29
b2World::SetDebugDraw
void SetDebugDraw(b2Draw *debugDraw)
b2World::GetTreeBalance
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
b2Fixture
Definition: b2_fixture.h:111
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:166
b2World::SetContinuousPhysics
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2_world.h:157
b2World::Dump
void Dump()
b2World::SetAutoClearForces
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2_world.h:330
b2BlockAllocator
Definition: b2_block_allocator.h:39
b2World::b2World
b2World(const b2Vec2 &gravity)
b2BodyDef
Definition: b2_body.h:54
b2World::GetContactList
b2Contact * GetContactList()
Definition: b2_world.h:290
b2World::~b2World
~b2World()
Destruct the world. All physics entities are destroyed and all heap memory is released.
b2World::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin)
b2World::GetGravity
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2_world.h:320
b2World::RayCast
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
b2Contact
Definition: b2_contact.h:81
b2World::SetSubStepping
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2_world.h:161
b2RayCastCallback
Definition: b2_world_callbacks.h:139
b2World
Definition: b2_world.h:45
b2ContactFilter
Definition: b2_world_callbacks.h:56
b2World::QueryAABB
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
b2World::GetProxyCount
int32 GetProxyCount() const
Get the number of broad-phase proxies.
b2World::GetProfile
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2_world.h:353
b2World::DrawDebugData
void DrawDebugData()
Call this to draw shapes and other debug draw data. This is intentionally non-const.
b2World::SetAllowSleeping
void SetAllowSleeping(bool flag)
Enable/disable sleep.
b2Joint
Definition: b2_joint.h:107
b2World::GetAutoClearForces
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2_world.h:343
b2DestructionListener
Definition: b2_world_callbacks.h:40
b2World::SetContactFilter
void SetContactFilter(b2ContactFilter *filter)
b2World::Step
void Step(float timeStep, int32 velocityIterations, int32 positionIterations)
b2World::IsLocked
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2_world.h:325