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 
113  void Step( float32 timeStep,
114  int32 velocityIterations,
115  int32 positionIterations,
116  int32 particleIterations);
117 
123  void Step( float32 timeStep,
124  int32 velocityIterations,
125  int32 positionIterations)
126  {
127  Step(timeStep, velocityIterations, positionIterations, 1);
128  }
129 
135  int CalculateReasonableParticleIterations(float32 timeStep) const;
136 
144  void ClearForces();
145 
147  void DrawDebugData();
148 
153  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
154 
160  void QueryShapeAABB(b2QueryCallback* callback, const b2Shape& shape,
161  const b2Transform& xf) const;
162 
169  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
170 
174  b2Body* GetBodyList();
175  const b2Body* GetBodyList() const;
176 
181  const b2Joint* GetJointList() const;
182 
189 
196  const b2Contact* GetContactList() const;
197 
199  void SetAllowSleeping(bool flag);
200  bool GetAllowSleeping() const { return m_allowSleep; }
201 
203  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
204  bool GetWarmStarting() const { return m_warmStarting; }
205 
207  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
208  bool GetContinuousPhysics() const { return m_continuousPhysics; }
209 
211  void SetSubStepping(bool flag) { m_subStepping = flag; }
212  bool GetSubStepping() const { return m_subStepping; }
213 
215  int32 GetProxyCount() const;
216 
218  int32 GetBodyCount() const;
219 
221  int32 GetJointCount() const;
222 
224  int32 GetContactCount() const;
225 
227  int32 GetTreeHeight() const;
228 
230  int32 GetTreeBalance() const;
231 
234  float32 GetTreeQuality() const;
235 
237  void SetGravity(const b2Vec2& gravity);
238 
240  b2Vec2 GetGravity() const;
241 
243  bool IsLocked() const;
244 
246  void SetAutoClearForces(bool flag);
247 
249  bool GetAutoClearForces() const;
250 
254  void ShiftOrigin(const b2Vec2& newOrigin);
255 
257  const b2ContactManager& GetContactManager() const;
258 
260  const b2Profile& GetProfile() const;
261 
264  void Dump();
265 
267  const b2Version* GetVersion() const {
268  return m_liquidFunVersion;
269  }
270 
272  const char* GetVersionString() const {
273  return m_liquidFunVersionString;
274  }
275 
276 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
277 public:
279  b2World(float32 gravityX, float32 gravityY);
280 
282  void SetGravity(float32 gravityX, float32 gravityY);
283 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
284 
285 private:
286 
287  // m_flags
288  enum
289  {
290  e_newFixture = 0x0001,
291  e_locked = 0x0002,
292  e_clearForces = 0x0004
293  };
294 
295  friend class b2Body;
296  friend class b2Fixture;
297  friend class b2ContactManager;
298  friend class b2Controller;
299  friend class b2ParticleSystem;
300 
301  void Init(const b2Vec2& gravity);
302 
303  void Solve(const b2TimeStep& step);
304  void SolveTOI(const b2TimeStep& step);
305 
306  void DrawJoint(b2Joint* joint);
307  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
308 
309  void DrawParticleSystem(const b2ParticleSystem& system);
310 
311  b2BlockAllocator m_blockAllocator;
312  b2StackAllocator m_stackAllocator;
313 
314  int32 m_flags;
315 
316  b2ContactManager m_contactManager;
317 
318  b2Body* m_bodyList;
319  b2Joint* m_jointList;
320  b2ParticleSystem* m_particleSystemList;
321 
322  int32 m_bodyCount;
323  int32 m_jointCount;
324 
325  b2Vec2 m_gravity;
326  bool m_allowSleep;
327 
328  b2DestructionListener* m_destructionListener;
329  b2Draw* m_debugDraw;
330 
331  // This is used to compute the time step ratio to
332  // support a variable time step.
333  float32 m_inv_dt0;
334 
335  // These are for debugging the solver.
336  bool m_warmStarting;
337  bool m_continuousPhysics;
338  bool m_subStepping;
339 
340  bool m_stepComplete;
341 
342  b2Profile m_profile;
343 
346  const b2Version *m_liquidFunVersion;
347  const char *m_liquidFunVersionString;
348 };
349 
351 {
352  return m_bodyList;
353 }
354 
355 inline const b2Body* b2World::GetBodyList() const
356 {
357  return m_bodyList;
358 }
359 
361 {
362  return m_jointList;
363 }
364 
365 inline const b2Joint* b2World::GetJointList() const
366 {
367  return m_jointList;
368 }
369 
371 {
372  return m_particleSystemList;
373 }
374 
376 {
377  return m_particleSystemList;
378 }
379 
381 {
382  return m_contactManager.m_contactList;
383 }
384 
385 inline const b2Contact* b2World::GetContactList() const
386 {
387  return m_contactManager.m_contactList;
388 }
389 
390 inline int32 b2World::GetBodyCount() const
391 {
392  return m_bodyCount;
393 }
394 
395 inline int32 b2World::GetJointCount() const
396 {
397  return m_jointCount;
398 }
399 
400 inline int32 b2World::GetContactCount() const
401 {
402  return m_contactManager.m_contactCount;
403 }
404 
405 inline void b2World::SetGravity(const b2Vec2& gravity)
406 {
407  m_gravity = gravity;
408 }
409 
411 {
412  return m_gravity;
413 }
414 
415 inline bool b2World::IsLocked() const
416 {
417  return (m_flags & e_locked) == e_locked;
418 }
419 
420 inline void b2World::SetAutoClearForces(bool flag)
421 {
422  if (flag)
423  {
424  m_flags |= e_clearForces;
425  }
426  else
427  {
428  m_flags &= ~e_clearForces;
429  }
430 }
431 
433 inline bool b2World::GetAutoClearForces() const
434 {
435  return (m_flags & e_clearForces) == e_clearForces;
436 }
437 
439 {
440  return m_contactManager;
441 }
442 
443 inline const b2Profile& b2World::GetProfile() const
444 {
445  return m_profile;
446 }
447 
448 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
449 inline b2World::b2World(float32 gravityX, float32 gravityY)
450 {
451  Init(b2Vec2(gravityX, gravityY));
452 }
453 
454 inline void b2World::SetGravity(float32 gravityX, float32 gravityY)
455 {
456  SetGravity(b2Vec2(gravityX, gravityY));
457 }
458 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
459 
460 #endif
Definition: b2Math.h:411
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2World.h:410
Definition: b2WorldCallbacks.h:41
Profiling data. Times are in milliseconds.
Definition: b2TimeStep.h:26
void SetDebugDraw(b2Draw *debugDraw)
Definition: b2World.cpp:88
b2ParticleSystem * CreateParticleSystem(const b2ParticleSystemDef *def)
Definition: b2World.cpp:353
Definition: b2ParticleSystem.h:153
b2Contact * GetContactList()
Definition: b2World.h:380
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
Definition: b2World.cpp:1410
Definition: b2WorldCallbacks.h:241
const b2Version * GetVersion() const
Get API version.
Definition: b2World.h:267
void SetContactFilter(b2ContactFilter *filter)
Definition: b2World.cpp:78
Definition: b2Body.h:52
void QueryShapeAABB(b2QueryCallback *callback, const b2Shape &shape, const b2Transform &xf) const
Definition: b2World.cpp:1089
Definition: b2StackAllocator.h:37
int32 GetProxyCount() const
Get the number of broad-phase proxies.
Definition: b2World.cpp:1400
Definition: b2World.h:44
Definition: b2ParticleSystem.h:281
float32 GetTreeQuality() const
Definition: b2World.cpp:1415
void DrawDebugData()
Call this to draw shapes and other debug draw data. This is intentionally non-const.
Definition: b2World.cpp:1264
Definition: b2WorldCallbacks.h:74
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2World.h:433
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: b2World.cpp:1420
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations, int32 particleIterations)
Definition: b2World.cpp:976
Definition: b2WorldCallbacks.h:128
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2World.cpp:198
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:350
void Dump()
Definition: b2World.cpp:1443
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2World.h:211
void DestroyJoint(b2Joint *joint)
Definition: b2World.cpp:258
Definition: b2Settings.h:244
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2World.h:207
void DestroyBody(b2Body *body)
Definition: b2World.cpp:117
A group of particles. b2ParticleGroup::CreateParticleGroup creates these.
Definition: b2ParticleGroup.h:172
int CalculateReasonableParticleIterations(float32 timeStep) const
Definition: b2World.cpp:1389
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:400
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2World.h:420
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:93
This is an internal structure.
Definition: b2TimeStep.h:39
void SetContactListener(b2ContactListener *listener)
Definition: b2World.cpp:83
b2Joint * GetJointList()
Definition: b2World.h:360
int32 GetJointCount() const
Get the number of joints.
Definition: b2World.h:395
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:390
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:132
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:43
void ClearForces()
Definition: b2World.cpp:1053
const char * GetVersionString() const
Get API version string.
Definition: b2World.h:272
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2World.h:203
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
Definition: b2World.cpp:1122
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.h:123
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2World.h:415
b2ParticleSystem * GetParticleSystemList()
Definition: b2World.h:370
void SetAllowSleeping(bool flag)
Enable/disable sleep.
Definition: b2World.cpp:406
A 2D column vector.
Definition: b2Math.h:56
Definition: b2Contact.h:77
void DestroyParticleSystem(b2ParticleSystem *p)
Definition: b2World.cpp:376
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2World.h:438
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2World.h:405
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2World.h:443
Definition: b2ContactManager.h:31
Definition: b2WorldCallbacks.h:208
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
Definition: b2World.cpp:1074
int32 GetTreeHeight() const
Get the height of the dynamic tree.
Definition: b2World.cpp:1405
Definition: b2Fixture.h:108
void SetDestructionListener(b2DestructionListener *listener)
Definition: b2World.cpp:73