![]() |
Box2D
2.2.0
A 2D Physics Engine for Games
|
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: 00047 b2World(const b2Vec2& gravity, bool doSleep); 00048 00050 ~b2World(); 00051 00054 void SetDestructionListener(b2DestructionListener* listener); 00055 00059 void SetContactFilter(b2ContactFilter* filter); 00060 00063 void SetContactListener(b2ContactListener* listener); 00064 00068 void SetDebugDraw(b2Draw* debugDraw); 00069 00073 b2Body* CreateBody(const b2BodyDef* def); 00074 00079 void DestroyBody(b2Body* body); 00080 00084 b2Joint* CreateJoint(const b2JointDef* def); 00085 00088 void DestroyJoint(b2Joint* joint); 00089 00095 void Step( float32 timeStep, 00096 int32 velocityIterations, 00097 int32 positionIterations); 00098 00106 void ClearForces(); 00107 00109 void DrawDebugData(); 00110 00115 void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const; 00116 00123 void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const; 00124 00128 b2Body* GetBodyList(); 00129 const b2Body* GetBodyList() const; 00130 00134 b2Joint* GetJointList(); 00135 const b2Joint* GetJointList() const; 00136 00142 b2Contact* GetContactList(); 00143 const b2Contact* GetContactList() const; 00144 00146 void SetWarmStarting(bool flag) { m_warmStarting = flag; } 00147 00149 void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; } 00150 00152 void SetSubStepping(bool flag) { m_subStepping = flag; } 00153 00155 int32 GetProxyCount() const; 00156 00158 int32 GetBodyCount() const; 00159 00161 int32 GetJointCount() const; 00162 00164 int32 GetContactCount() const; 00165 00167 int32 GetTreeHeight() const; 00168 00170 int32 GetTreeBalance() const; 00171 00174 float32 GetTreeQuality() const; 00175 00177 void SetGravity(const b2Vec2& gravity); 00178 00180 b2Vec2 GetGravity() const; 00181 00183 bool IsLocked() const; 00184 00186 void SetAutoClearForces(bool flag); 00187 00189 bool GetAutoClearForces() const; 00190 00192 const b2ContactManager& GetContactManager() const; 00193 00195 const b2Profile& GetProfile() const; 00196 00197 private: 00198 00199 // m_flags 00200 enum 00201 { 00202 e_newFixture = 0x0001, 00203 e_locked = 0x0002, 00204 e_clearForces = 0x0004 00205 }; 00206 00207 friend class b2Body; 00208 friend class b2Fixture; 00209 friend class b2ContactManager; 00210 friend class b2Controller; 00211 00212 void Solve(const b2TimeStep& step); 00213 void SolveTOI(const b2TimeStep& step); 00214 00215 void DrawJoint(b2Joint* joint); 00216 void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color); 00217 00218 b2BlockAllocator m_blockAllocator; 00219 b2StackAllocator m_stackAllocator; 00220 00221 int32 m_flags; 00222 00223 b2ContactManager m_contactManager; 00224 00225 b2Body* m_bodyList; 00226 b2Joint* m_jointList; 00227 00228 int32 m_bodyCount; 00229 int32 m_jointCount; 00230 00231 b2Vec2 m_gravity; 00232 bool m_allowSleep; 00233 00234 b2DestructionListener* m_destructionListener; 00235 b2Draw* m_debugDraw; 00236 00237 // This is used to compute the time step ratio to 00238 // support a variable time step. 00239 float32 m_inv_dt0; 00240 00241 // These are for debugging the solver. 00242 bool m_warmStarting; 00243 bool m_continuousPhysics; 00244 bool m_subStepping; 00245 00246 bool m_stepComplete; 00247 00248 b2Profile m_profile; 00249 }; 00250 00251 inline b2Body* b2World::GetBodyList() 00252 { 00253 return m_bodyList; 00254 } 00255 00256 inline const b2Body* b2World::GetBodyList() const 00257 { 00258 return m_bodyList; 00259 } 00260 00261 inline b2Joint* b2World::GetJointList() 00262 { 00263 return m_jointList; 00264 } 00265 00266 inline const b2Joint* b2World::GetJointList() const 00267 { 00268 return m_jointList; 00269 } 00270 00271 inline b2Contact* b2World::GetContactList() 00272 { 00273 return m_contactManager.m_contactList; 00274 } 00275 00276 inline const b2Contact* b2World::GetContactList() const 00277 { 00278 return m_contactManager.m_contactList; 00279 } 00280 00281 inline int32 b2World::GetBodyCount() const 00282 { 00283 return m_bodyCount; 00284 } 00285 00286 inline int32 b2World::GetJointCount() const 00287 { 00288 return m_jointCount; 00289 } 00290 00291 inline int32 b2World::GetContactCount() const 00292 { 00293 return m_contactManager.m_contactCount; 00294 } 00295 00296 inline void b2World::SetGravity(const b2Vec2& gravity) 00297 { 00298 m_gravity = gravity; 00299 } 00300 00301 inline b2Vec2 b2World::GetGravity() const 00302 { 00303 return m_gravity; 00304 } 00305 00306 inline bool b2World::IsLocked() const 00307 { 00308 return (m_flags & e_locked) == e_locked; 00309 } 00310 00311 inline void b2World::SetAutoClearForces(bool flag) 00312 { 00313 if (flag) 00314 { 00315 m_flags |= e_clearForces; 00316 } 00317 else 00318 { 00319 m_flags &= ~e_clearForces; 00320 } 00321 } 00322 00324 inline bool b2World::GetAutoClearForces() const 00325 { 00326 return (m_flags & e_clearForces) == e_clearForces; 00327 } 00328 00329 inline const b2ContactManager& b2World::GetContactManager() const 00330 { 00331 return m_contactManager; 00332 } 00333 00334 inline const b2Profile& b2World::GetProfile() const 00335 { 00336 return m_profile; 00337 } 00338 00339 #endif