Box2D  2.3.0
A 2D Physics Engine for Games
b2World.h
1 /*
2 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef B2_WORLD_H
20 #define B2_WORLD_H
21 
22 #include <Box2D/Common/b2Math.h>
23 #include <Box2D/Common/b2BlockAllocator.h>
24 #include <Box2D/Common/b2StackAllocator.h>
25 #include <Box2D/Dynamics/b2ContactManager.h>
26 #include <Box2D/Dynamics/b2WorldCallbacks.h>
27 #include <Box2D/Dynamics/b2TimeStep.h>
28 
29 struct b2AABB;
30 struct b2BodyDef;
31 struct b2Color;
32 struct b2JointDef;
33 class b2Body;
34 class b2Draw;
35 class b2Fixture;
36 class b2Joint;
37 
41 class b2World
42 {
43 public:
46  b2World(const b2Vec2& gravity);
47 
49  ~b2World();
50 
54 
58  void SetContactFilter(b2ContactFilter* filter);
59 
62  void SetContactListener(b2ContactListener* listener);
63 
67  void SetDebugDraw(b2Draw* debugDraw);
68 
72  b2Body* CreateBody(const b2BodyDef* def);
73 
78  void DestroyBody(b2Body* body);
79 
83  b2Joint* CreateJoint(const b2JointDef* def);
84 
87  void DestroyJoint(b2Joint* joint);
88 
94  void Step( float32 timeStep,
95  int32 velocityIterations,
96  int32 positionIterations);
97 
105  void ClearForces();
106 
108  void DrawDebugData();
109 
114  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
115 
122  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
123 
127  b2Body* GetBodyList();
128  const b2Body* GetBodyList() const;
129 
134  const b2Joint* GetJointList() const;
135 
142  const b2Contact* GetContactList() const;
143 
145  void SetAllowSleeping(bool flag);
146  bool GetAllowSleeping() const { return m_allowSleep; }
147 
149  void SetWarmStarting(bool flag) { m_warmStarting = flag; }
150  bool GetWarmStarting() const { return m_warmStarting; }
151 
153  void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
154  bool GetContinuousPhysics() const { return m_continuousPhysics; }
155 
157  void SetSubStepping(bool flag) { m_subStepping = flag; }
158  bool GetSubStepping() const { return m_subStepping; }
159 
161  int32 GetProxyCount() const;
162 
164  int32 GetBodyCount() const;
165 
167  int32 GetJointCount() const;
168 
170  int32 GetContactCount() const;
171 
173  int32 GetTreeHeight() const;
174 
176  int32 GetTreeBalance() const;
177 
180  float32 GetTreeQuality() const;
181 
183  void SetGravity(const b2Vec2& gravity);
184 
186  b2Vec2 GetGravity() const;
187 
189  bool IsLocked() const;
190 
192  void SetAutoClearForces(bool flag);
193 
195  bool GetAutoClearForces() const;
196 
200  void ShiftOrigin(const b2Vec2& newOrigin);
201 
203  const b2ContactManager& GetContactManager() const;
204 
206  const b2Profile& GetProfile() const;
207 
210  void Dump();
211 
212 private:
213 
214  // m_flags
215  enum
216  {
217  e_newFixture = 0x0001,
218  e_locked = 0x0002,
219  e_clearForces = 0x0004
220  };
221 
222  friend class b2Body;
223  friend class b2Fixture;
224  friend class b2ContactManager;
225  friend class b2Controller;
226 
227  void Solve(const b2TimeStep& step);
228  void SolveTOI(const b2TimeStep& step);
229 
230  void DrawJoint(b2Joint* joint);
231  void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
232 
233  b2BlockAllocator m_blockAllocator;
234  b2StackAllocator m_stackAllocator;
235 
236  int32 m_flags;
237 
238  b2ContactManager m_contactManager;
239 
240  b2Body* m_bodyList;
241  b2Joint* m_jointList;
242 
243  int32 m_bodyCount;
244  int32 m_jointCount;
245 
246  b2Vec2 m_gravity;
247  bool m_allowSleep;
248 
249  b2DestructionListener* m_destructionListener;
250  b2Draw* m_debugDraw;
251 
252  // This is used to compute the time step ratio to
253  // support a variable time step.
254  float32 m_inv_dt0;
255 
256  // These are for debugging the solver.
257  bool m_warmStarting;
258  bool m_continuousPhysics;
259  bool m_subStepping;
260 
261  bool m_stepComplete;
262 
263  b2Profile m_profile;
264 };
265 
267 {
268  return m_bodyList;
269 }
270 
271 inline const b2Body* b2World::GetBodyList() const
272 {
273  return m_bodyList;
274 }
275 
277 {
278  return m_jointList;
279 }
280 
281 inline const b2Joint* b2World::GetJointList() const
282 {
283  return m_jointList;
284 }
285 
287 {
288  return m_contactManager.m_contactList;
289 }
290 
291 inline const b2Contact* b2World::GetContactList() const
292 {
293  return m_contactManager.m_contactList;
294 }
295 
296 inline int32 b2World::GetBodyCount() const
297 {
298  return m_bodyCount;
299 }
300 
301 inline int32 b2World::GetJointCount() const
302 {
303  return m_jointCount;
304 }
305 
306 inline int32 b2World::GetContactCount() const
307 {
308  return m_contactManager.m_contactCount;
309 }
310 
311 inline void b2World::SetGravity(const b2Vec2& gravity)
312 {
313  m_gravity = gravity;
314 }
315 
317 {
318  return m_gravity;
319 }
320 
321 inline bool b2World::IsLocked() const
322 {
323  return (m_flags & e_locked) == e_locked;
324 }
325 
326 inline void b2World::SetAutoClearForces(bool flag)
327 {
328  if (flag)
329  {
330  m_flags |= e_clearForces;
331  }
332  else
333  {
334  m_flags &= ~e_clearForces;
335  }
336 }
337 
339 inline bool b2World::GetAutoClearForces() const
340 {
341  return (m_flags & e_clearForces) == e_clearForces;
342 }
343 
345 {
346  return m_contactManager;
347 }
348 
349 inline const b2Profile& b2World::GetProfile() const
350 {
351  return m_profile;
352 }
353 
354 #endif