Warning, file /education/gcompris/external/qml-box2d/Box2D/Dynamics/b2World.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_WORLD_H
0020 #define B2_WORLD_H
0021 
0022 #include <Box2D/Common/b2Math.h>
0023 #include <Box2D/Common/b2BlockAllocator.h>
0024 #include <Box2D/Common/b2StackAllocator.h>
0025 #include <Box2D/Dynamics/b2ContactManager.h>
0026 #include <Box2D/Dynamics/b2WorldCallbacks.h>
0027 #include <Box2D/Dynamics/b2TimeStep.h>
0028 
0029 struct b2AABB;
0030 struct b2BodyDef;
0031 struct b2Color;
0032 struct b2JointDef;
0033 class b2Body;
0034 class b2Draw;
0035 class b2Fixture;
0036 class b2Joint;
0037 
0038 /// The world class manages all physics entities, dynamic simulation,
0039 /// and asynchronous queries. The world also contains efficient memory
0040 /// management facilities.
0041 class b2World
0042 {
0043 public:
0044     /// Construct a world object.
0045     /// @param gravity the world gravity vector.
0046     b2World(const b2Vec2& gravity);
0047 
0048     /// Destruct the world. All physics entities are destroyed and all heap memory is released.
0049     ~b2World();
0050 
0051     /// Register a destruction listener. The listener is owned by you and must
0052     /// remain in scope.
0053     void SetDestructionListener(b2DestructionListener* listener);
0054 
0055     /// Register a contact filter to provide specific control over collision.
0056     /// Otherwise the default filter is used (b2_defaultFilter). The listener is
0057     /// owned by you and must remain in scope. 
0058     void SetContactFilter(b2ContactFilter* filter);
0059 
0060     /// Register a contact event listener. The listener is owned by you and must
0061     /// remain in scope.
0062     void SetContactListener(b2ContactListener* listener);
0063 
0064     /// Register a routine for debug drawing. The debug draw functions are called
0065     /// inside with b2World::DrawDebugData method. The debug draw object is owned
0066     /// by you and must remain in scope.
0067     void SetDebugDraw(b2Draw* debugDraw);
0068 
0069     /// Create a rigid body given a definition. No reference to the definition
0070     /// is retained.
0071     /// @warning This function is locked during callbacks.
0072     b2Body* CreateBody(const b2BodyDef* def);
0073 
0074     /// Destroy a rigid body given a definition. No reference to the definition
0075     /// is retained. This function is locked during callbacks.
0076     /// @warning This automatically deletes all associated shapes and joints.
0077     /// @warning This function is locked during callbacks.
0078     void DestroyBody(b2Body* body);
0079 
0080     /// Create a joint to constrain bodies together. No reference to the definition
0081     /// is retained. This may cause the connected bodies to cease colliding.
0082     /// @warning This function is locked during callbacks.
0083     b2Joint* CreateJoint(const b2JointDef* def);
0084 
0085     /// Destroy a joint. This may cause the connected bodies to begin colliding.
0086     /// @warning This function is locked during callbacks.
0087     void DestroyJoint(b2Joint* joint);
0088 
0089     /// Take a time step. This performs collision detection, integration,
0090     /// and constraint solution.
0091     /// @param timeStep the amount of time to simulate, this should not vary.
0092     /// @param velocityIterations for the velocity constraint solver.
0093     /// @param positionIterations for the position constraint solver.
0094     void Step(  float32 timeStep,
0095                 int32 velocityIterations,
0096                 int32 positionIterations);
0097 
0098     /// Manually clear the force buffer on all bodies. By default, forces are cleared automatically
0099     /// after each call to Step. The default behavior is modified by calling SetAutoClearForces.
0100     /// The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain
0101     /// a fixed sized time step under a variable frame-rate.
0102     /// When you perform sub-stepping you will disable auto clearing of forces and instead call
0103     /// ClearForces after all sub-steps are complete in one pass of your game loop.
0104     /// @see SetAutoClearForces
0105     void ClearForces();
0106 
0107     /// Call this to draw shapes and other debug draw data. This is intentionally non-const.
0108     void DrawDebugData();
0109 
0110     /// Query the world for all fixtures that potentially overlap the
0111     /// provided AABB.
0112     /// @param callback a user implemented callback class.
0113     /// @param aabb the query box.
0114     void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
0115 
0116     /// Ray-cast the world for all fixtures in the path of the ray. Your callback
0117     /// controls whether you get the closest point, any point, or n-points.
0118     /// The ray-cast ignores shapes that contain the starting point.
0119     /// @param callback a user implemented callback class.
0120     /// @param point1 the ray starting point
0121     /// @param point2 the ray ending point
0122     void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
0123 
0124     /// Get the world body list. With the returned body, use b2Body::GetNext to get
0125     /// the next body in the world list. A NULL body indicates the end of the list.
0126     /// @return the head of the world body list.
0127     b2Body* GetBodyList();
0128     const b2Body* GetBodyList() const;
0129 
0130     /// Get the world joint list. With the returned joint, use b2Joint::GetNext to get
0131     /// the next joint in the world list. A NULL joint indicates the end of the list.
0132     /// @return the head of the world joint list.
0133     b2Joint* GetJointList();
0134     const b2Joint* GetJointList() const;
0135 
0136     /// Get the world contact list. With the returned contact, use b2Contact::GetNext to get
0137     /// the next contact in the world list. A NULL contact indicates the end of the list.
0138     /// @return the head of the world contact list.
0139     /// @warning contacts are created and destroyed in the middle of a time step.
0140     /// Use b2ContactListener to avoid missing contacts.
0141     b2Contact* GetContactList();
0142     const b2Contact* GetContactList() const;
0143 
0144     /// Enable/disable sleep.
0145     void SetAllowSleeping(bool flag);
0146     bool GetAllowSleeping() const { return m_allowSleep; }
0147 
0148     /// Enable/disable warm starting. For testing.
0149     void SetWarmStarting(bool flag) { m_warmStarting = flag; }
0150     bool GetWarmStarting() const { return m_warmStarting; }
0151 
0152     /// Enable/disable continuous physics. For testing.
0153     void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
0154     bool GetContinuousPhysics() const { return m_continuousPhysics; }
0155 
0156     /// Enable/disable single stepped continuous physics. For testing.
0157     void SetSubStepping(bool flag) { m_subStepping = flag; }
0158     bool GetSubStepping() const { return m_subStepping; }
0159 
0160     /// Get the number of broad-phase proxies.
0161     int32 GetProxyCount() const;
0162 
0163     /// Get the number of bodies.
0164     int32 GetBodyCount() const;
0165 
0166     /// Get the number of joints.
0167     int32 GetJointCount() const;
0168 
0169     /// Get the number of contacts (each may have 0 or more contact points).
0170     int32 GetContactCount() const;
0171 
0172     /// Get the height of the dynamic tree.
0173     int32 GetTreeHeight() const;
0174 
0175     /// Get the balance of the dynamic tree.
0176     int32 GetTreeBalance() const;
0177 
0178     /// Get the quality metric of the dynamic tree. The smaller the better.
0179     /// The minimum is 1.
0180     float32 GetTreeQuality() const;
0181 
0182     /// Change the global gravity vector.
0183     void SetGravity(const b2Vec2& gravity);
0184     
0185     /// Get the global gravity vector.
0186     b2Vec2 GetGravity() const;
0187 
0188     /// Is the world locked (in the middle of a time step).
0189     bool IsLocked() const;
0190 
0191     /// Set flag to control automatic clearing of forces after each time step.
0192     void SetAutoClearForces(bool flag);
0193 
0194     /// Get the flag that controls automatic clearing of forces after each time step.
0195     bool GetAutoClearForces() const;
0196 
0197     /// Shift the world origin. Useful for large worlds.
0198     /// The body shift formula is: position -= newOrigin
0199     /// @param newOrigin the new origin with respect to the old origin
0200     void ShiftOrigin(const b2Vec2& newOrigin);
0201 
0202     /// Get the contact manager for testing.
0203     const b2ContactManager& GetContactManager() const;
0204 
0205     /// Get the current profile.
0206     const b2Profile& GetProfile() const;
0207 
0208     /// Dump the world into the log file.
0209     /// @warning this should be called outside of a time step.
0210     void Dump();
0211 
0212 private:
0213 
0214     // m_flags
0215     enum
0216     {
0217         e_newFixture    = 0x0001,
0218         e_locked        = 0x0002,
0219         e_clearForces   = 0x0004
0220     };
0221 
0222     friend class b2Body;
0223     friend class b2Fixture;
0224     friend class b2ContactManager;
0225     friend class b2Controller;
0226 
0227     void Solve(const b2TimeStep& step);
0228     void SolveTOI(const b2TimeStep& step);
0229 
0230     void DrawJoint(b2Joint* joint);
0231     void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
0232 
0233     b2BlockAllocator m_blockAllocator;
0234     b2StackAllocator m_stackAllocator;
0235 
0236     int32 m_flags;
0237 
0238     b2ContactManager m_contactManager;
0239 
0240     b2Body* m_bodyList;
0241     b2Joint* m_jointList;
0242 
0243     int32 m_bodyCount;
0244     int32 m_jointCount;
0245 
0246     b2Vec2 m_gravity;
0247     bool m_allowSleep;
0248 
0249     b2DestructionListener* m_destructionListener;
0250     b2Draw* g_debugDraw;
0251 
0252     // This is used to compute the time step ratio to
0253     // support a variable time step.
0254     float32 m_inv_dt0;
0255 
0256     // These are for debugging the solver.
0257     bool m_warmStarting;
0258     bool m_continuousPhysics;
0259     bool m_subStepping;
0260 
0261     bool m_stepComplete;
0262 
0263     b2Profile m_profile;
0264 };
0265 
0266 inline b2Body* b2World::GetBodyList()
0267 {
0268     return m_bodyList;
0269 }
0270 
0271 inline const b2Body* b2World::GetBodyList() const
0272 {
0273     return m_bodyList;
0274 }
0275 
0276 inline b2Joint* b2World::GetJointList()
0277 {
0278     return m_jointList;
0279 }
0280 
0281 inline const b2Joint* b2World::GetJointList() const
0282 {
0283     return m_jointList;
0284 }
0285 
0286 inline b2Contact* b2World::GetContactList()
0287 {
0288     return m_contactManager.m_contactList;
0289 }
0290 
0291 inline const b2Contact* b2World::GetContactList() const
0292 {
0293     return m_contactManager.m_contactList;
0294 }
0295 
0296 inline int32 b2World::GetBodyCount() const
0297 {
0298     return m_bodyCount;
0299 }
0300 
0301 inline int32 b2World::GetJointCount() const
0302 {
0303     return m_jointCount;
0304 }
0305 
0306 inline int32 b2World::GetContactCount() const
0307 {
0308     return m_contactManager.m_contactCount;
0309 }
0310 
0311 inline void b2World::SetGravity(const b2Vec2& gravity)
0312 {
0313     m_gravity = gravity;
0314 }
0315 
0316 inline b2Vec2 b2World::GetGravity() const
0317 {
0318     return m_gravity;
0319 }
0320 
0321 inline bool b2World::IsLocked() const
0322 {
0323     return (m_flags & e_locked) == e_locked;
0324 }
0325 
0326 inline void b2World::SetAutoClearForces(bool flag)
0327 {
0328     if (flag)
0329     {
0330         m_flags |= e_clearForces;
0331     }
0332     else
0333     {
0334         m_flags &= ~e_clearForces;
0335     }
0336 }
0337 
0338 /// Get the flag that controls automatic clearing of forces after each time step.
0339 inline bool b2World::GetAutoClearForces() const
0340 {
0341     return (m_flags & e_clearForces) == e_clearForces;
0342 }
0343 
0344 inline const b2ContactManager& b2World::GetContactManager() const
0345 {
0346     return m_contactManager;
0347 }
0348 
0349 inline const b2Profile& b2World::GetProfile() const
0350 {
0351     return m_profile;
0352 }
0353 
0354 #endif