Box2D: b2World.h Source File

Box2D 2.3.0 A 2D Physics Engine for Games

- Main Page
- Classes
- Files

- File List
- File Members

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
53 void SetDestructionListener(b2DestructionListener* listener);
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
133 b2Joint* GetJointList();
134 const b2Joint* GetJointList() const;
135
141 b2Contact* GetContactList();
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* g_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
266 inline b2Body* b2World::GetBodyList()
267 {
268 return m_bodyList;
269 }
270
271 inline const b2Body* b2World::GetBodyList() const
272 {
273 return m_bodyList;
274 }
275
276 inline b2Joint* b2World::GetJointList()
277 {
278 return m_jointList;
279 }
280
281 inline const b2Joint* b2World::GetJointList() const
282 {
283 return m_jointList;
284 }
285
286 inline b2Contact* b2World::GetContactList()
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
316 inline b2Vec2 b2World::GetGravity() const
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
344 inline const b2ContactManager& b2World::GetContactManager() const
345 {
346 return m_contactManager;
347 }
348
349 inline const b2Profile& b2World::GetProfile() const
350 {
351 return m_profile;
352 }
353
354 #endif
b2Transform
Definition: b2Math.h:350
b2World::GetGravity
b2Vec2 GetGravity() const
Get the global gravity vector.
Definition: b2World.h:316
b2DestructionListener
Definition: b2WorldCallbacks.h:36
b2Profile
Profiling data. Times are in milliseconds.
Definition: b2TimeStep.h:25
b2World::SetDebugDraw
void SetDebugDraw(b2Draw *debugDraw)
Definition: b2World.cpp:102
b2World::GetContactList
b2Contact * GetContactList()
Definition: b2World.h:286
b2World::GetTreeBalance
int32 GetTreeBalance() const
Get the balance of the dynamic tree.
Definition: b2World.cpp:1249
b2RayCastCallback
Definition: b2WorldCallbacks.h:135
b2World::SetContactFilter
void SetContactFilter(b2ContactFilter *filter)
Definition: b2World.cpp:92
b2BodyDef
Definition: b2Body.h:51
b2StackAllocator
Definition: b2StackAllocator.h:37
b2World::GetProxyCount
int32 GetProxyCount() const
Get the number of broad-phase proxies.
Definition: b2World.cpp:1239
b2World
Definition: b2World.h:41
b2World::GetTreeQuality
float32 GetTreeQuality() const
Definition: b2World.cpp:1254
b2World::DrawDebugData
void DrawDebugData()
Call this to draw shapes and other debug draw data. This is intentionally non-const.
Definition: b2World.cpp:1135
b2ContactFilter
Definition: b2WorldCallbacks.h:52
b2World::GetAutoClearForces
bool GetAutoClearForces() const
Get the flag that controls automatic clearing of forces after each time step.
Definition: b2World.h:339
b2World::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: b2World.cpp:1259
b2ContactListener
Definition: b2WorldCallbacks.h:81
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2World.cpp:212
b2BlockAllocator
Definition: b2BlockAllocator.h:35
b2Color
Color for debug drawing. Each value has the range [0,1].
Definition: b2Draw.h:25
b2Joint
Definition: b2Joint.h:103
b2World::GetBodyList
b2Body * GetBodyList()
Definition: b2World.h:266
b2World::Dump
void Dump()
Definition: b2World.cpp:1282
b2World::SetSubStepping
void SetSubStepping(bool flag)
Enable/disable single stepped continuous physics. For testing.
Definition: b2World.h:157
b2World::DestroyJoint
void DestroyJoint(b2Joint *joint)
Definition: b2World.cpp:272
b2World::SetContinuousPhysics
void SetContinuousPhysics(bool flag)
Enable/disable continuous physics. For testing.
Definition: b2World.h:153
b2World::DestroyBody
void DestroyBody(b2Body *body)
Definition: b2World.cpp:131
b2AABB
An axis aligned bounding box.
Definition: b2Collision.h:162
b2World::GetContactCount
int32 GetContactCount() const
Get the number of contacts (each may have 0 or more contact points).
Definition: b2World.h:306
b2World::SetAutoClearForces
void SetAutoClearForces(bool flag)
Set flag to control automatic clearing of forces after each time step.
Definition: b2World.h:326
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:107
b2TimeStep
This is an internal structure.
Definition: b2TimeStep.h:38
b2World::SetContactListener
void SetContactListener(b2ContactListener *listener)
Definition: b2World.cpp:97
b2World::GetJointList
b2Joint * GetJointList()
Definition: b2World.h:276
b2World::GetJointCount
int32 GetJointCount() const
Get the number of joints.
Definition: b2World.h:301
b2JointDef
Joint definitions are used to construct joints.
Definition: b2Joint.h:74
b2Draw
Definition: b2Draw.h:35
b2World::GetBodyCount
int32 GetBodyCount() const
Get the number of bodies.
Definition: b2World.h:296
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
b2World::b2World
b2World(const b2Vec2 &gravity)
Definition: b2World.cpp:37
b2World::~b2World
~b2World()
Destruct the world. All physics entities are destroyed and all heap memory is released.
Definition: b2World.cpp:66
b2World::ClearForces
void ClearForces()
Definition: b2World.cpp:965
b2World::SetWarmStarting
void SetWarmStarting(bool flag)
Enable/disable warm starting. For testing.
Definition: b2World.h:149
b2World::RayCast
void RayCast(b2RayCastCallback *callback, const b2Vec2 &point1, const b2Vec2 &point2) const
Definition: b2World.cpp:1019
b2World::Step
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.cpp:897
b2World::IsLocked
bool IsLocked() const
Is the world locked (in the middle of a time step).
Definition: b2World.h:321
b2World::SetAllowSleeping
void SetAllowSleeping(bool flag)
Enable/disable sleep.
Definition: b2World.cpp:368
b2Vec2
A 2D column vector.
Definition: b2Math.h:53
b2Contact
Definition: b2Contact.h:77
b2World::GetContactManager
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2World.h:344
b2World::SetGravity
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition: b2World.h:311
b2World::GetProfile
const b2Profile & GetProfile() const
Get the current profile.
Definition: b2World.h:349
b2ContactManager
Definition: b2ContactManager.h:30
b2QueryCallback
Definition: b2WorldCallbacks.h:123
b2World::QueryAABB
void QueryAABB(b2QueryCallback *callback, const b2AABB &aabb) const
Definition: b2World.cpp:986
b2World::GetTreeHeight
int32 GetTreeHeight() const
Get the height of the dynamic tree.
Definition: b2World.cpp:1244
b2Fixture
Definition: b2Fixture.h:107
b2World::SetDestructionListener
void SetDestructionListener(b2DestructionListener *listener)
Definition: b2World.cpp:87