LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2World.h
1 /*
2 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3 * Copyright (c) 2013 Google, Inc.
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 * 2. Altered source versions must be plainly marked as such, and must not be
16 * misrepresented as being the original software.
17 * 3. This notice may not be removed or altered from any source distribution.
18 */
19 
20 #ifndef B2_WORLD_H
21 #define B2_WORLD_H
22 
23 #include <Box2D/Common/b2Math.h>
24 #include <Box2D/Common/b2BlockAllocator.h>
25 #include <Box2D/Common/b2StackAllocator.h>
26 #include <Box2D/Dynamics/b2ContactManager.h>
27 #include <Box2D/Dynamics/b2WorldCallbacks.h>
28 #include <Box2D/Dynamics/b2TimeStep.h>
29 #include <Box2D/Particle/b2ParticleSystem.h>
30 
31 struct b2AABB;
32 struct b2BodyDef;
33 struct b2Color;
34 struct b2JointDef;
35 class b2Body;
36 class b2Draw;
37 class b2Fixture;
38 class b2Joint;
39 class b2ParticleGroup;
40 
44 class b2World
45 {
46 public:
49  b2World(const b2Vec2& gravity);
50 
52  ~b2World();
53 
57 
61  void SetContactFilter(b2ContactFilter* filter);
62 
65  void SetContactListener(b2ContactListener* listener);
66 
70  void SetDebugDraw(b2Draw* debugDraw);
71 
75  b2Body* CreateBody(const b2BodyDef* def);
76 
81  void DestroyBody(b2Body* body);
82 
86  b2Joint* CreateJoint(const b2JointDef* def);
87 
90  void DestroyJoint(b2Joint* joint);
91 
96 
100 
112  void Step( float32 timeStep,
113  int32 velocityIterations,
114  int32 positionIterations,
115  int32 particleIterations);
116 
122  void Step( float32 timeStep,
123  int32 velocityIterations,
124  int32 positionIterations)
125  {
126  Step(timeStep, velocityIterations, positionIterations, 1);
127  }
128 
136  void ClearForces();
137 
139  void DrawDebugData();
140 
145  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
146 
152  void QueryShapeAABB(b2QueryCallback* callback, const b2Shape& shape,
153  const b2Transform& xf) const;
154 
161  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
162 
166  b2Body* GetBodyList();
167  const b2Body* GetBodyList() const;
168 
173  const b2Joint* GetJointList() const;
174 
181 
188  const b2Contact* GetContactList() const;
189 
191  void SetAllowSleeping(bool flag);
192  bool GetAllowSleeping() const { return m_allowSleep; }
193 
195  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
196  bool GetWarmStarting() const { return m_warmStarting; }
197 
199  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
200  bool GetContinuousPhysics() const { return m_continuousPhysics; }
201 
203  void SetSubStepping(bool flag) { m_subStepping = flag; }
204  bool GetSubStepping() const { return m_subStepping; }
205 
207  int32 GetProxyCount() const;
208 
210  int32 GetBodyCount() const;
211 
213  int32 GetJointCount() const;
214 
216  int32 GetContactCount() const;
217 
219  int32 GetTreeHeight() const;
220 
222  int32 GetTreeBalance() const;
223 
226  float32 GetTreeQuality() const;
227 
229  void SetGravity(const b2Vec2& gravity);
230 
232  b2Vec2 GetGravity() const;
233 
235  bool IsLocked() const;
236 
238  void SetAutoClearForces(bool flag);
239 
241  bool GetAutoClearForces() const;
242 
246  void ShiftOrigin(const b2Vec2& newOrigin);
247 
249  const b2ContactManager& GetContactManager() const;
250 
252  const b2Profile& GetProfile() const;
253 
256  void Dump();
257 
259  const b2Version* GetVersion() const {
260  return m_liquidFunVersion;
261  }
262 
264  const char* GetVersionString() const {
265  return m_liquidFunVersionString;
266  }
267 
268 private:
269 
270  // m_flags
271  enum
272  {
273  e_newFixture = 0x0001,
274  e_locked = 0x0002,
275  e_clearForces = 0x0004
276  };
277 
278  friend class b2Body;
279  friend class b2Fixture;
280  friend class b2ContactManager;
281  friend class b2Controller;
282  friend class b2ParticleSystem;
283 
284  void Solve(const b2TimeStep& step);
285  void SolveTOI(const b2TimeStep& step);
286 
287  void DrawJoint(b2Joint* joint);
288  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
289 
290  void DrawParticleSystem(const b2ParticleSystem& system);
291 
292  b2BlockAllocator m_blockAllocator;
293  b2StackAllocator m_stackAllocator;
294 
295  int32 m_flags;
296 
297  b2ContactManager m_contactManager;
298 
299  b2Body* m_bodyList;
300  b2Joint* m_jointList;
301  b2ParticleSystem* m_particleSystemList;
302 
303  int32 m_bodyCount;
304  int32 m_jointCount;
305 
306  b2Vec2 m_gravity;
307  bool m_allowSleep;
308 
309  b2DestructionListener* m_destructionListener;
310  b2Draw* m_debugDraw;
311 
312  // This is used to compute the time step ratio to
313  // support a variable time step.
314  float32 m_inv_dt0;
315 
316  // These are for debugging the solver.
317  bool m_warmStarting;
318  bool m_continuousPhysics;
319  bool m_subStepping;
320 
321  bool m_stepComplete;
322 
323  b2Profile m_profile;
324 
327  const b2Version *m_liquidFunVersion;
328  const char *m_liquidFunVersionString;
329 };
330 
332 {
333  return m_bodyList;
334 }
335 
336 inline const b2Body* b2World::GetBodyList() const
337 {
338  return m_bodyList;
339 }
340 
342 {
343  return m_jointList;
344 }
345 
346 inline const b2Joint* b2World::GetJointList() const
347 {
348  return m_jointList;
349 }
350 
352 {
353  return m_particleSystemList;
354 }
355 
357 {
358  return m_particleSystemList;
359 }
360 
362 {
363  return m_contactManager.m_contactList;
364 }
365 
366 inline const b2Contact* b2World::GetContactList() const
367 {
368  return m_contactManager.m_contactList;
369 }
370 
371 inline int32 b2World::GetBodyCount() const
372 {
373  return m_bodyCount;
374 }
375 
376 inline int32 b2World::GetJointCount() const
377 {
378  return m_jointCount;
379 }
380 
381 inline int32 b2World::GetContactCount() const
382 {
383  return m_contactManager.m_contactCount;
384 }
385 
386 inline void b2World::SetGravity(const b2Vec2& gravity)
387 {
388  m_gravity = gravity;
389 }
390 
392 {
393  return m_gravity;
394 }
395 
396 inline bool b2World::IsLocked() const
397 {
398  return (m_flags & e_locked) == e_locked;
399 }
400 
401 inline void b2World::SetAutoClearForces(bool flag)
402 {
403  if (flag)
404  {
405  m_flags |= e_clearForces;
406  }
407  else
408  {
409  m_flags &= ~e_clearForces;
410  }
411 }
412 
414 inline bool b2World::GetAutoClearForces() const
415 {
416  return (m_flags & e_clearForces) == e_clearForces;
417 }
418 
420 {
421  return m_contactManager;
422 }
423 
424 inline const b2Profile& b2World::GetProfile() const
425 {
426  return m_profile;
427 }
428 
429 #endif
Definition: b2Math.h:411
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2World.h:391
Definition: b2WorldCallbacks.h:41
Profiling data. Times are in milliseconds.
Definition: b2TimeStep.h:26
void SetDebugDraw(b2Draw *debugDraw)
Definition: b2World.cpp:116
b2ParticleSystem * CreateParticleSystem(const b2ParticleSystemDef *def)
Definition: b2World.cpp:381
Definition: b2ParticleSystem.h:84
b2Contact * GetContactList()
Definition: b2World.h:361
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
Definition: b2World.cpp:1381
Definition: b2WorldCallbacks.h:241
const b2Version * GetVersion() const
Get API version.
Definition: b2World.h:259
void SetContactFilter(b2ContactFilter *filter)
Definition: b2World.cpp:106
Definition: b2Body.h:52
void QueryShapeAABB(b2QueryCallback *callback, const b2Shape &shape, const b2Transform &xf) const
Definition: b2World.cpp:1083
Definition: b2StackAllocator.h:37
int32 GetProxyCount() const
Get the number of broad-phase proxies.
Definition: b2World.cpp:1371
Definition: b2World.h:44
Definition: b2ParticleSystem.h:189
float32 GetTreeQuality() const
Definition: b2World.cpp:1386
void DrawDebugData()
Call this to draw shapes and other debug draw data. This is intentionally non-const.
Definition: b2World.cpp:1258
Definition: b2WorldCallbacks.h:74
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2World.h:414
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: b2World.cpp:1391
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations, int32 particleIterations)
Definition: b2World.cpp:970
Definition: b2WorldCallbacks.h:128
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2World.cpp:226
Definition: b2BlockAllocator.h:36
Color for debug drawing. Each value has the range [0,1].
Definition: b2Draw.h:27
Definition: b2Joint.h:103
Definition: b2Shape.h:43
b2Body * GetBodyList()
Definition: b2World.h:331
void Dump()
Definition: b2World.cpp:1414
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2World.h:203
void DestroyJoint(b2Joint *joint)
Definition: b2World.cpp:286
Definition: b2Settings.h:223
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2World.h:199
void DestroyBody(b2Body *body)
Definition: b2World.cpp:145
A group of particles. b2ParticleGroup::CreateParticleGroup creates these.
Definition: b2ParticleGroup.h:134
An axis aligned bounding box.
Definition: b2Collision.h:162
int32 GetContactCount() const
Get the number of contacts (each may have 0 or more contact points).
Definition: b2World.h:381
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2World.h:401
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:121
This is an internal structure.
Definition: b2TimeStep.h:39
void SetContactListener(b2ContactListener *listener)
Definition: b2World.cpp:111
b2Joint * GetJointList()
Definition: b2World.h:341
int32 GetJointCount() const
Get the number of joints.
Definition: b2World.h:376
Joint definitions are used to construct joints.
Definition: b2Joint.h:74
Definition: b2Draw.h:37
int32 GetBodyCount() const
Get the number of bodies.
Definition: b2World.h:371
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:127
b2World(const b2Vec2 &gravity)
Definition: b2World.cpp:38
~b2World()
Destruct the world. All physics entities are destroyed and all heap memory is released.
Definition: b2World.cpp:71
void ClearForces()
Definition: b2World.cpp:1047
const char * GetVersionString() const
Get API version string.
Definition: b2World.h:264
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2World.h:195
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
Definition: b2World.cpp:1116
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.h:122
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2World.h:396
b2ParticleSystem * GetParticleSystemList()
Definition: b2World.h:351
void SetAllowSleeping(bool flag)
Enable/disable sleep.
Definition: b2World.cpp:434
A 2D column vector.
Definition: b2Math.h:56
Definition: b2Contact.h:77
void DestroyParticleSystem(b2ParticleSystem *p)
Definition: b2World.cpp:404
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2World.h:419
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2World.h:386
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2World.h:424
Definition: b2ContactManager.h:31
Definition: b2WorldCallbacks.h:208
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
Definition: b2World.cpp:1068
int32 GetTreeHeight() const
Get the height of the dynamic tree.
Definition: b2World.cpp:1376
Definition: b2Fixture.h:108
void SetDestructionListener(b2DestructionListener *listener)
Definition: b2World.cpp:101