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 00200 void ShiftOrigin(const b2Vec2& newOrigin); 00201 00203 const b2ContactManager& GetContactManager() const; 00204 00206 const b2Profile& GetProfile() const; 00207 00210 void Dump(); 00211 00212 private: 00213 00214 // m_flags 00215 enum 00216 { 00217 e_newFixture = 0x0001, 00218 e_locked = 0x0002, 00219 e_clearForces = 0x0004 00220 }; 00221 00222 friend class b2Body; 00223 friend class b2Fixture; 00224 friend class b2ContactManager; 00225 friend class b2Controller; 00226 00227 void Solve(const b2TimeStep& step); 00228 void SolveTOI(const b2TimeStep& step); 00229 00230 void DrawJoint(b2Joint* joint); 00231 void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color); 00232 00233 b2BlockAllocator m_blockAllocator; 00234 b2StackAllocator m_stackAllocator; 00235 00236 int32 m_flags; 00237 00238 b2ContactManager m_contactManager; 00239 00240 b2Body* m_bodyList; 00241 b2Joint* m_jointList; 00242 00243 int32 m_bodyCount; 00244 int32 m_jointCount; 00245 00246 b2Vec2 m_gravity; 00247 bool m_allowSleep; 00248 00249 b2DestructionListener* m_destructionListener; 00250 b2Draw* g_debugDraw; 00251 00252 // This is used to compute the time step ratio to 00253 // support a variable time step. 00254 float32 m_inv_dt0; 00255 00256 // These are for debugging the solver. 00257 bool m_warmStarting; 00258 bool m_continuousPhysics; 00259 bool m_subStepping; 00260 00261 bool m_stepComplete; 00262 00263 b2Profile m_profile; 00264 }; 00265 00266 inline b2Body* b2World::GetBodyList() 00267 { 00268 return m_bodyList; 00269 } 00270 00271 inline const b2Body* b2World::GetBodyList() const 00272 { 00273 return m_bodyList; 00274 } 00275 00276 inline b2Joint* b2World::GetJointList() 00277 { 00278 return m_jointList; 00279 } 00280 00281 inline const b2Joint* b2World::GetJointList() const 00282 { 00283 return m_jointList; 00284 } 00285 00286 inline b2Contact* b2World::GetContactList() 00287 { 00288 return m_contactManager.m_contactList; 00289 } 00290 00291 inline const b2Contact* b2World::GetContactList() const 00292 { 00293 return m_contactManager.m_contactList; 00294 } 00295 00296 inline int32 b2World::GetBodyCount() const 00297 { 00298 return m_bodyCount; 00299 } 00300 00301 inline int32 b2World::GetJointCount() const 00302 { 00303 return m_jointCount; 00304 } 00305 00306 inline int32 b2World::GetContactCount() const 00307 { 00308 return m_contactManager.m_contactCount; 00309 } 00310 00311 inline void b2World::SetGravity(const b2Vec2& gravity) 00312 { 00313 m_gravity = gravity; 00314 } 00315 00316 inline b2Vec2 b2World::GetGravity() const 00317 { 00318 return m_gravity; 00319 } 00320 00321 inline bool b2World::IsLocked() const 00322 { 00323 return (m_flags & e_locked) == e_locked; 00324 } 00325 00326 inline void b2World::SetAutoClearForces(bool flag) 00327 { 00328 if (flag) 00329 { 00330 m_flags |= e_clearForces; 00331 } 00332 else 00333 { 00334 m_flags &= ~e_clearForces; 00335 } 00336 } 00337 00339 inline bool b2World::GetAutoClearForces() const 00340 { 00341 return (m_flags & e_clearForces) == e_clearForces; 00342 } 00343 00344 inline const b2ContactManager& b2World::GetContactManager() const 00345 { 00346 return m_contactManager; 00347 } 00348 00349 inline const b2Profile& b2World::GetProfile() const 00350 { 00351 return m_profile; 00352 } 00353 00354 #endif