Box2D  2.2.1
A 2D Physics Engine for Games
b2World.h
00001 /*
00002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #ifndef B2_WORLD_H
00020 #define B2_WORLD_H
00021 
00022 #include <Box2D/Common/b2Math.h>
00023 #include <Box2D/Common/b2BlockAllocator.h>
00024 #include <Box2D/Common/b2StackAllocator.h>
00025 #include <Box2D/Dynamics/b2ContactManager.h>
00026 #include <Box2D/Dynamics/b2WorldCallbacks.h>
00027 #include <Box2D/Dynamics/b2TimeStep.h>
00028 
00029 struct b2AABB;
00030 struct b2BodyDef;
00031 struct b2Color;
00032 struct b2JointDef;
00033 class b2Body;
00034 class b2Draw;
00035 class b2Fixture;
00036 class b2Joint;
00037 
00041 class b2World
00042 {
00043 public:
00046         b2World(const b2Vec2& gravity);
00047 
00049         ~b2World();
00050 
00053         void SetDestructionListener(b2DestructionListener* listener);
00054 
00058         void SetContactFilter(b2ContactFilter* filter);
00059 
00062         void SetContactListener(b2ContactListener* listener);
00063 
00067         void SetDebugDraw(b2Draw* debugDraw);
00068 
00072         b2Body* CreateBody(const b2BodyDef* def);
00073 
00078         void DestroyBody(b2Body* body);
00079 
00083         b2Joint* CreateJoint(const b2JointDef* def);
00084 
00087         void DestroyJoint(b2Joint* joint);
00088 
00094         void Step(      float32 timeStep,
00095                                 int32 velocityIterations,
00096                                 int32 positionIterations);
00097 
00105         void ClearForces();
00106 
00108         void DrawDebugData();
00109 
00114         void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
00115 
00122         void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
00123 
00127         b2Body* GetBodyList();
00128         const b2Body* GetBodyList() const;
00129 
00133         b2Joint* GetJointList();
00134         const b2Joint* GetJointList() const;
00135 
00141         b2Contact* GetContactList();
00142         const b2Contact* GetContactList() const;
00143 
00145         void SetAllowSleeping(bool flag);
00146         bool GetAllowSleeping() const { return m_allowSleep; }
00147 
00149         void SetWarmStarting(bool flag) { m_warmStarting = flag; }
00150         bool GetWarmStarting() const { return m_warmStarting; }
00151 
00153         void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
00154         bool GetContinuousPhysics() const { return m_continuousPhysics; }
00155 
00157         void SetSubStepping(bool flag) { m_subStepping = flag; }
00158         bool GetSubStepping() const { return m_subStepping; }
00159 
00161         int32 GetProxyCount() const;
00162 
00164         int32 GetBodyCount() const;
00165 
00167         int32 GetJointCount() const;
00168 
00170         int32 GetContactCount() const;
00171 
00173         int32 GetTreeHeight() const;
00174 
00176         int32 GetTreeBalance() const;
00177 
00180         float32 GetTreeQuality() const;
00181 
00183         void SetGravity(const b2Vec2& gravity);
00184         
00186         b2Vec2 GetGravity() const;
00187 
00189         bool IsLocked() const;
00190 
00192         void SetAutoClearForces(bool flag);
00193 
00195         bool GetAutoClearForces() const;
00196 
00198         const b2ContactManager& GetContactManager() const;
00199 
00201         const b2Profile& GetProfile() const;
00202 
00205         void Dump();
00206 
00207 private:
00208 
00209         // m_flags
00210         enum
00211         {
00212                 e_newFixture    = 0x0001,
00213                 e_locked                = 0x0002,
00214                 e_clearForces   = 0x0004
00215         };
00216 
00217         friend class b2Body;
00218         friend class b2Fixture;
00219         friend class b2ContactManager;
00220         friend class b2Controller;
00221 
00222         void Solve(const b2TimeStep& step);
00223         void SolveTOI(const b2TimeStep& step);
00224 
00225         void DrawJoint(b2Joint* joint);
00226         void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
00227 
00228         b2BlockAllocator m_blockAllocator;
00229         b2StackAllocator m_stackAllocator;
00230 
00231         int32 m_flags;
00232 
00233         b2ContactManager m_contactManager;
00234 
00235         b2Body* m_bodyList;
00236         b2Joint* m_jointList;
00237 
00238         int32 m_bodyCount;
00239         int32 m_jointCount;
00240 
00241         b2Vec2 m_gravity;
00242         bool m_allowSleep;
00243 
00244         b2DestructionListener* m_destructionListener;
00245         b2Draw* m_debugDraw;
00246 
00247         // This is used to compute the time step ratio to
00248         // support a variable time step.
00249         float32 m_inv_dt0;
00250 
00251         // These are for debugging the solver.
00252         bool m_warmStarting;
00253         bool m_continuousPhysics;
00254         bool m_subStepping;
00255 
00256         bool m_stepComplete;
00257 
00258         b2Profile m_profile;
00259 };
00260 
00261 inline b2Body* b2World::GetBodyList()
00262 {
00263         return m_bodyList;
00264 }
00265 
00266 inline const b2Body* b2World::GetBodyList() const
00267 {
00268         return m_bodyList;
00269 }
00270 
00271 inline b2Joint* b2World::GetJointList()
00272 {
00273         return m_jointList;
00274 }
00275 
00276 inline const b2Joint* b2World::GetJointList() const
00277 {
00278         return m_jointList;
00279 }
00280 
00281 inline b2Contact* b2World::GetContactList()
00282 {
00283         return m_contactManager.m_contactList;
00284 }
00285 
00286 inline const b2Contact* b2World::GetContactList() const
00287 {
00288         return m_contactManager.m_contactList;
00289 }
00290 
00291 inline int32 b2World::GetBodyCount() const
00292 {
00293         return m_bodyCount;
00294 }
00295 
00296 inline int32 b2World::GetJointCount() const
00297 {
00298         return m_jointCount;
00299 }
00300 
00301 inline int32 b2World::GetContactCount() const
00302 {
00303         return m_contactManager.m_contactCount;
00304 }
00305 
00306 inline void b2World::SetGravity(const b2Vec2& gravity)
00307 {
00308         m_gravity = gravity;
00309 }
00310 
00311 inline b2Vec2 b2World::GetGravity() const
00312 {
00313         return m_gravity;
00314 }
00315 
00316 inline bool b2World::IsLocked() const
00317 {
00318         return (m_flags & e_locked) == e_locked;
00319 }
00320 
00321 inline void b2World::SetAutoClearForces(bool flag)
00322 {
00323         if (flag)
00324         {
00325                 m_flags |= e_clearForces;
00326         }
00327         else
00328         {
00329                 m_flags &= ~e_clearForces;
00330         }
00331 }
00332 
00334 inline bool b2World::GetAutoClearForces() const
00335 {
00336         return (m_flags & e_clearForces) == e_clearForces;
00337 }
00338 
00339 inline const b2ContactManager& b2World::GetContactManager() const
00340 {
00341         return m_contactManager;
00342 }
00343 
00344 inline const b2Profile& b2World::GetProfile() const
00345 {
00346         return m_profile;
00347 }
00348 
00349 #endif
 All Classes Files Functions Variables Enumerations Enumerator Defines