LiquidFun
 All Classes Files Functions Variables 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 
97  void Step( float32 timeStep,
98  int32 velocityIterations,
99  int32 positionIterations);
100 
108  void ClearForces();
109 
111  void DrawDebugData();
112 
117  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
118 
125  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
126 
130  b2Body* GetBodyList();
131  const b2Body* GetBodyList() const;
132 
137  const b2Joint* GetJointList() const;
138 
145  const b2Contact* GetContactList() const;
146 
148  void SetAllowSleeping(bool flag);
149  bool GetAllowSleeping() const { return m_allowSleep; }
150 
152  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
153  bool GetWarmStarting() const { return m_warmStarting; }
154 
156  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
157  bool GetContinuousPhysics() const { return m_continuousPhysics; }
158 
160  void SetSubStepping(bool flag) { m_subStepping = flag; }
161  bool GetSubStepping() const { return m_subStepping; }
162 
164  int32 GetProxyCount() const;
165 
167  int32 GetBodyCount() const;
168 
170  int32 GetJointCount() const;
171 
173  int32 GetContactCount() const;
174 
176  int32 GetTreeHeight() const;
177 
179  int32 GetTreeBalance() const;
180 
183  float32 GetTreeQuality() const;
184 
186  void SetGravity(const b2Vec2& gravity);
187 
189  b2Vec2 GetGravity() const;
190 
192  bool IsLocked() const;
193 
195  void SetAutoClearForces(bool flag);
196 
198  bool GetAutoClearForces() const;
199 
203  void ShiftOrigin(const b2Vec2& newOrigin);
204 
206  const b2ContactManager& GetContactManager() const;
207 
209  const b2Profile& GetProfile() const;
210 
213  void Dump();
214 
222  int32 CreateParticle(const b2ParticleDef& def);
223 
226  void DestroyParticle(int32 index)
227  {
228  DestroyParticle(index, false);
229  }
230 
236  void DestroyParticle(int32 index, bool callDestructionListener);
237 
247  int32 DestroyParticlesInShape(const b2Shape& shape, const b2Transform& xf)
248  {
249  return DestroyParticlesInShape(shape, xf, false);
250  }
251 
263  int32 DestroyParticlesInShape(const b2Shape& shape, const b2Transform& xf,
264  bool callDestructionListener);
265 
266 
271 
276  void JoinParticleGroups(b2ParticleGroup* groupA, b2ParticleGroup* groupB);
277 
284  void DestroyParticlesInGroup(b2ParticleGroup* group, bool callDestructionListener);
285 
292  {
293  DestroyParticlesInGroup(group, false);
294  }
295 
301  const b2ParticleGroup* GetParticleGroupList() const;
302 
304  int32 GetParticleGroupCount() const;
305 
307  int32 GetParticleCount() const;
308 
310  int32 GetParticleMaxCount() const;
311 
313  void SetParticleMaxCount(int32 count);
314 
316  void SetParticleDensity(float32 density);
317 
319  float32 GetParticleDensity() const;
320 
323  void SetParticleGravityScale(float32 gravityScale);
324 
326  float32 GetParticleGravityScale() const;
327 
331  void SetParticleDamping(float32 damping);
332 
334  float32 GetParticleDamping() const;
335 
339  void SetParticleRadius(float32 radius);
340 
342  float32 GetParticleRadius() const;
343 
346  uint32* GetParticleFlagsBuffer();
347  b2Vec2* GetParticlePositionBuffer();
348  b2Vec2* GetParticleVelocityBuffer();
349  b2ParticleColor* GetParticleColorBuffer();
350  b2ParticleGroup* const* GetParticleGroupBuffer();
351  void** GetParticleUserDataBuffer();
352  const uint32* GetParticleFlagsBuffer() const;
353  const b2Vec2* GetParticlePositionBuffer() const;
354  const b2Vec2* GetParticleVelocityBuffer() const;
355  const b2ParticleColor* GetParticleColorBuffer() const;
356  const b2ParticleGroup* const* GetParticleGroupBuffer() const;
357  void* const* GetParticleUserDataBuffer() const;
358 
362  void SetParticleFlagsBuffer(uint32* buffer, int32 capacity);
363  void SetParticlePositionBuffer(b2Vec2* buffer, int32 capacity);
364  void SetParticleVelocityBuffer(b2Vec2* buffer, int32 capacity);
365  void SetParticleColorBuffer(b2ParticleColor* buffer, int32 capacity);
366  void SetParticleUserDataBuffer(void** buffer, int32 capacity);
367 
370  int32 GetParticleContactCount();
371 
374  int32 GetParticleBodyContactCount();
375 
377  float32 ComputeParticleCollisionEnergy() const;
378 
380  const b2Version* GetVersion() const {
381  return m_liquidFunVersion;
382  }
383 
385  const char* GetVersionString() const {
386  return m_liquidFunVersionString;
387  }
388 
389 private:
390 
391  // m_flags
392  enum
393  {
394  e_newFixture = 0x0001,
395  e_locked = 0x0002,
396  e_clearForces = 0x0004
397  };
398 
399  friend class b2Body;
400  friend class b2Fixture;
401  friend class b2ContactManager;
402  friend class b2Controller;
403  friend class b2ParticleSystem;
404 
405  void Solve(const b2TimeStep& step);
406  void SolveTOI(const b2TimeStep& step);
407 
408  void DrawJoint(b2Joint* joint);
409  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
410 
411  void DrawParticleSystem(const b2ParticleSystem& system);
412 
413  b2BlockAllocator m_blockAllocator;
414  b2StackAllocator m_stackAllocator;
415 
416  int32 m_flags;
417 
418  b2ContactManager m_contactManager;
419 
420  b2Body* m_bodyList;
421  b2Joint* m_jointList;
422 
423  int32 m_bodyCount;
424  int32 m_jointCount;
425 
426  b2Vec2 m_gravity;
427  bool m_allowSleep;
428 
429  b2DestructionListener* m_destructionListener;
430  b2Draw* m_debugDraw;
431 
432  // This is used to compute the time step ratio to
433  // support a variable time step.
434  float32 m_inv_dt0;
435 
436  // These are for debugging the solver.
437  bool m_warmStarting;
438  bool m_continuousPhysics;
439  bool m_subStepping;
440 
441  bool m_stepComplete;
442 
443  b2Profile m_profile;
444 
445  b2ParticleSystem m_particleSystem;
446 
449  const b2Version *m_liquidFunVersion;
450  const char *m_liquidFunVersionString;
451 };
452 
454 {
455  return m_bodyList;
456 }
457 
458 inline const b2Body* b2World::GetBodyList() const
459 {
460  return m_bodyList;
461 }
462 
464 {
465  return m_jointList;
466 }
467 
468 inline const b2Joint* b2World::GetJointList() const
469 {
470  return m_jointList;
471 }
472 
474 {
475  return m_contactManager.m_contactList;
476 }
477 
478 inline const b2Contact* b2World::GetContactList() const
479 {
480  return m_contactManager.m_contactList;
481 }
482 
483 inline int32 b2World::GetBodyCount() const
484 {
485  return m_bodyCount;
486 }
487 
488 inline int32 b2World::GetJointCount() const
489 {
490  return m_jointCount;
491 }
492 
493 inline int32 b2World::GetContactCount() const
494 {
495  return m_contactManager.m_contactCount;
496 }
497 
498 inline void b2World::SetGravity(const b2Vec2& gravity)
499 {
500  m_gravity = gravity;
501 }
502 
504 {
505  return m_gravity;
506 }
507 
508 inline bool b2World::IsLocked() const
509 {
510  return (m_flags & e_locked) == e_locked;
511 }
512 
513 inline void b2World::SetAutoClearForces(bool flag)
514 {
515  if (flag)
516  {
517  m_flags |= e_clearForces;
518  }
519  else
520  {
521  m_flags &= ~e_clearForces;
522  }
523 }
524 
526 inline bool b2World::GetAutoClearForces() const
527 {
528  return (m_flags & e_clearForces) == e_clearForces;
529 }
530 
532 {
533  return m_contactManager;
534 }
535 
536 inline const b2Profile& b2World::GetProfile() const
537 {
538  return m_profile;
539 }
540 
542 {
543  return m_particleSystem.GetParticleGroupList();
544 }
545 
547 {
548  return m_particleSystem.GetParticleGroupList();
549 }
550 
551 inline int32 b2World::GetParticleGroupCount() const
552 {
553  return m_particleSystem.GetParticleGroupCount();
554 }
555 
556 inline int32 b2World::GetParticleCount() const
557 {
558  return m_particleSystem.GetParticleCount();
559 }
560 
561 #endif
Definition: b2Math.h:361
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2World.h:503
Definition: b2WorldCallbacks.h:38
Profiling data. Times are in milliseconds.
Definition: b2TimeStep.h:25
int32 GetParticleMaxCount() const
Get the maximum number of particles.
Definition: b2World.cpp:1377
int32 GetParticleCount() const
Get the number of particles.
Definition: b2World.h:556
void SetDebugDraw(b2Draw *debugDraw)
Definition: b2World.cpp:109
Definition: b2ParticleGroup.h:37
b2Contact * GetContactList()
Definition: b2World.h:473
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
Definition: b2World.cpp:1285
Definition: b2WorldCallbacks.h:160
float32 GetParticleGravityScale() const
Get the particle gravity scale.
Definition: b2World.cpp:1402
const b2Version * GetVersion() const
Get API version.
Definition: b2World.h:380
void SetContactFilter(b2ContactFilter *filter)
Definition: b2World.cpp:99
Definition: b2Body.h:52
void SetParticleGravityScale(float32 gravityScale)
Definition: b2World.cpp:1397
void SetParticleRadius(float32 radius)
Definition: b2World.cpp:1417
Definition: b2StackAllocator.h:37
int32 GetProxyCount() const
Get the number of broad-phase proxies.
Definition: b2World.cpp:1275
float32 GetParticleDensity() const
Get the particle density.
Definition: b2World.cpp:1392
Definition: b2World.h:44
Definition: b2ParticleSystem.h:74
float32 GetTreeQuality() const
Definition: b2World.cpp:1290
void DrawDebugData()
Call this to draw shapes and other debug draw data.
Definition: b2World.cpp:1170
Definition: b2WorldCallbacks.h:69
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2World.h:526
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: b2World.cpp:1295
Definition: b2WorldCallbacks.h:98
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2World.cpp:219
Definition: b2BlockAllocator.h:35
Color for debug drawing. Each value has the range [0,1].
Definition: b2Draw.h:26
Definition: b2Joint.h:103
Definition: b2Shape.h:43
b2Body * GetBodyList()
Definition: b2World.h:453
void Dump()
Definition: b2World.cpp:1318
void DestroyParticlesInGroup(b2ParticleGroup *group)
Definition: b2World.h:291
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2World.h:160
Definition: b2Particle.h:85
void DestroyJoint(b2Joint *joint)
Definition: b2World.cpp:279
int32 GetParticleGroupCount() const
Get the number of particle groups.
Definition: b2World.h:551
void JoinParticleGroups(b2ParticleGroup *groupA, b2ParticleGroup *groupB)
Definition: b2World.cpp:1466
Definition: b2Settings.h:168
void SetParticleMaxCount(int32 count)
Set the maximum number of particles.
Definition: b2World.cpp:1382
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2World.h:156
float32 GetParticleRadius() const
Get the particle radius.
Definition: b2World.cpp:1422
void DestroyBody(b2Body *body)
Definition: b2World.cpp:138
A group of particles. These are created via b2World::CreateParticleGroup.
Definition: b2ParticleGroup.h:93
float32 GetParticleDamping() const
Get damping for particles.
Definition: b2World.cpp:1412
An axis aligned bounding box.
Definition: b2Collision.h:161
int32 GetContactCount() const
Get the number of contacts (each may have 0 or more contact points).
Definition: b2World.h:493
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2World.h:513
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:114
This is an internal structure.
Definition: b2TimeStep.h:38
void SetParticleDamping(float32 damping)
Definition: b2World.cpp:1407
const b2ParticleBodyContact * GetParticleBodyContacts()
Get contacts between particles and bodies.
Definition: b2World.cpp:1581
void SetContactListener(b2ContactListener *listener)
Definition: b2World.cpp:104
b2Joint * GetJointList()
Definition: b2World.h:463
int32 GetJointCount() const
Get the number of joints.
Definition: b2World.h:488
Joint definitions are used to construct joints.
Definition: b2Joint.h:74
Definition: b2Draw.h:36
Definition: b2ParticleSystem.h:55
int32 GetBodyCount() const
Get the number of bodies.
Definition: b2World.h:483
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:127
void SetParticleFlagsBuffer(uint32 *buffer, int32 capacity)
Definition: b2World.cpp:1541
b2World(const b2Vec2 &gravity)
Definition: b2World.cpp:38
b2ParticleGroup * GetParticleGroupList()
Definition: b2World.h:541
const b2ParticleContact * GetParticleContacts()
Get contacts between particles.
Definition: b2World.cpp:1571
~b2World()
Destruct the world. All physics entities are destroyed and all heap memory is released.
Definition: b2World.cpp:73
void ClearForces()
Definition: b2World.cpp:979
int32 CreateParticle(const b2ParticleDef &def)
Definition: b2World.cpp:1427
const char * GetVersionString() const
Get API version string.
Definition: b2World.h:385
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2World.h:152
void DestroyParticlesInGroup(b2ParticleGroup *group, bool callDestructionListener)
Definition: b2World.cpp:1476
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
Definition: b2World.cpp:1034
void DestroyParticle(int32 index)
Definition: b2World.h:226
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.cpp:910
Small color object for each particle.
Definition: b2Particle.h:42
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2World.h:508
float32 ComputeParticleCollisionEnergy() const
Compute the kinetic energy that can be lost by damping force.
Definition: b2World.cpp:1591
void SetAllowSleeping(bool flag)
Enable/disable sleep.
Definition: b2World.cpp:375
uint32 * GetParticleFlagsBuffer()
Definition: b2World.cpp:1486
Definition: b2ParticleSystem.h:39
A 2D column vector.
Definition: b2Math.h:64
Definition: b2Contact.h:77
void SetParticleDensity(float32 density)
Change the particle density.
Definition: b2World.cpp:1387
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2World.h:531
b2ParticleGroup * CreateParticleGroup(const b2ParticleGroupDef &def)
Definition: b2World.cpp:1455
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2World.h:498
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2World.h:536
Definition: b2ContactManager.h:30
Definition: b2WorldCallbacks.h:140
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
Definition: b2World.cpp:1000
int32 GetTreeHeight() const
Get the height of the dynamic tree.
Definition: b2World.cpp:1280
int32 DestroyParticlesInShape(const b2Shape &shape, const b2Transform &xf)
Definition: b2World.h:247
Definition: b2Fixture.h:108
void SetDestructionListener(b2DestructionListener *listener)
Definition: b2World.cpp:94