b2World.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #ifndef B2_WORLD_H
  19. #define B2_WORLD_H
  20. #include <Box2D/Common/b2Math.h>
  21. #include <Box2D/Common/b2BlockAllocator.h>
  22. #include <Box2D/Common/b2StackAllocator.h>
  23. #include <Box2D/Dynamics/b2ContactManager.h>
  24. #include <Box2D/Dynamics/b2WorldCallbacks.h>
  25. #include <Box2D/Dynamics/b2TimeStep.h>
  26. struct b2AABB;
  27. struct b2BodyDef;
  28. struct b2Color;
  29. struct b2JointDef;
  30. class b2Body;
  31. class b2Draw;
  32. class b2Fixture;
  33. class b2Joint;
  34. /// The world class manages all physics entities, dynamic simulation,
  35. /// and asynchronous queries. The world also contains efficient memory
  36. /// management facilities.
  37. class b2World
  38. {
  39. public:
  40. /// Construct a world object.
  41. /// @param gravity the world gravity vector.
  42. b2World(const b2Vec2& gravity);
  43. /// Destruct the world. All physics entities are destroyed and all heap memory is released.
  44. ~b2World();
  45. /// Register a destruction listener. The listener is owned by you and must
  46. /// remain in scope.
  47. void SetDestructionListener(b2DestructionListener* listener);
  48. /// Register a contact filter to provide specific control over collision.
  49. /// Otherwise the default filter is used (b2_defaultFilter). The listener is
  50. /// owned by you and must remain in scope.
  51. void SetContactFilter(b2ContactFilter* filter);
  52. /// Register a contact event listener. The listener is owned by you and must
  53. /// remain in scope.
  54. void SetContactListener(b2ContactListener* listener);
  55. /// Register a routine for debug drawing. The debug draw functions are called
  56. /// inside with b2World::DrawDebugData method. The debug draw object is owned
  57. /// by you and must remain in scope.
  58. void SetDebugDraw(b2Draw* debugDraw);
  59. /// Create a rigid body given a definition. No reference to the definition
  60. /// is retained.
  61. /// @warning This function is locked during callbacks.
  62. b2Body* CreateBody(const b2BodyDef* def);
  63. /// Destroy a rigid body given a definition. No reference to the definition
  64. /// is retained. This function is locked during callbacks.
  65. /// @warning This automatically deletes all associated shapes and joints.
  66. /// @warning This function is locked during callbacks.
  67. void DestroyBody(b2Body* body);
  68. /// Create a joint to constrain bodies together. No reference to the definition
  69. /// is retained. This may cause the connected bodies to cease colliding.
  70. /// @warning This function is locked during callbacks.
  71. b2Joint* CreateJoint(const b2JointDef* def);
  72. /// Destroy a joint. This may cause the connected bodies to begin colliding.
  73. /// @warning This function is locked during callbacks.
  74. void DestroyJoint(b2Joint* joint);
  75. /// Take a time step. This performs collision detection, integration,
  76. /// and constraint solution.
  77. /// @param timeStep the amount of time to simulate, this should not vary.
  78. /// @param velocityIterations for the velocity constraint solver.
  79. /// @param positionIterations for the position constraint solver.
  80. void Step( float32 timeStep,
  81. int32 velocityIterations,
  82. int32 positionIterations);
  83. /// Manually clear the force buffer on all bodies. By default, forces are cleared automatically
  84. /// after each call to Step. The default behavior is modified by calling SetAutoClearForces.
  85. /// The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain
  86. /// a fixed sized time step under a variable frame-rate.
  87. /// When you perform sub-stepping you will disable auto clearing of forces and instead call
  88. /// ClearForces after all sub-steps are complete in one pass of your game loop.
  89. /// @see SetAutoClearForces
  90. void ClearForces();
  91. /// Call this to draw shapes and other debug draw data. This is intentionally non-const.
  92. void DrawDebugData();
  93. /// Query the world for all fixtures that potentially overlap the
  94. /// provided AABB.
  95. /// @param callback a user implemented callback class.
  96. /// @param aabb the query box.
  97. void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
  98. /// Ray-cast the world for all fixtures in the path of the ray. Your callback
  99. /// controls whether you get the closest point, any point, or n-points.
  100. /// The ray-cast ignores shapes that contain the starting point.
  101. /// @param callback a user implemented callback class.
  102. /// @param point1 the ray starting point
  103. /// @param point2 the ray ending point
  104. void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
  105. /// Get the world body list. With the returned body, use b2Body::GetNext to get
  106. /// the next body in the world list. A NULL body indicates the end of the list.
  107. /// @return the head of the world body list.
  108. b2Body* GetBodyList();
  109. const b2Body* GetBodyList() const;
  110. /// Get the world joint list. With the returned joint, use b2Joint::GetNext to get
  111. /// the next joint in the world list. A NULL joint indicates the end of the list.
  112. /// @return the head of the world joint list.
  113. b2Joint* GetJointList();
  114. const b2Joint* GetJointList() const;
  115. /// Get the world contact list. With the returned contact, use b2Contact::GetNext to get
  116. /// the next contact in the world list. A NULL contact indicates the end of the list.
  117. /// @return the head of the world contact list.
  118. /// @warning contacts are created and destroyed in the middle of a time step.
  119. /// Use b2ContactListener to avoid missing contacts.
  120. b2Contact* GetContactList();
  121. const b2Contact* GetContactList() const;
  122. /// Enable/disable sleep.
  123. void SetAllowSleeping(bool flag);
  124. bool GetAllowSleeping() const { return m_allowSleep; }
  125. /// Enable/disable warm starting. For testing.
  126. void SetWarmStarting(bool flag) { m_warmStarting = flag; }
  127. bool GetWarmStarting() const { return m_warmStarting; }
  128. /// Enable/disable continuous physics. For testing.
  129. void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
  130. bool GetContinuousPhysics() const { return m_continuousPhysics; }
  131. /// Enable/disable single stepped continuous physics. For testing.
  132. void SetSubStepping(bool flag) { m_subStepping = flag; }
  133. bool GetSubStepping() const { return m_subStepping; }
  134. /// Get the number of broad-phase proxies.
  135. int32 GetProxyCount() const;
  136. /// Get the number of bodies.
  137. int32 GetBodyCount() const;
  138. /// Get the number of joints.
  139. int32 GetJointCount() const;
  140. /// Get the number of contacts (each may have 0 or more contact points).
  141. int32 GetContactCount() const;
  142. /// Get the height of the dynamic tree.
  143. int32 GetTreeHeight() const;
  144. /// Get the balance of the dynamic tree.
  145. int32 GetTreeBalance() const;
  146. /// Get the quality metric of the dynamic tree. The smaller the better.
  147. /// The minimum is 1.
  148. float32 GetTreeQuality() const;
  149. /// Change the global gravity vector.
  150. void SetGravity(const b2Vec2& gravity);
  151. /// Get the global gravity vector.
  152. b2Vec2 GetGravity() const;
  153. /// Is the world locked (in the middle of a time step).
  154. bool IsLocked() const;
  155. /// Set flag to control automatic clearing of forces after each time step.
  156. void SetAutoClearForces(bool flag);
  157. /// Get the flag that controls automatic clearing of forces after each time step.
  158. bool GetAutoClearForces() const;
  159. /// Shift the world origin. Useful for large worlds.
  160. /// The body shift formula is: position -= newOrigin
  161. /// @param newOrigin the new origin with respect to the old origin
  162. void ShiftOrigin(const b2Vec2& newOrigin);
  163. /// Get the contact manager for testing.
  164. const b2ContactManager& GetContactManager() const;
  165. /// Get the current profile.
  166. const b2Profile& GetProfile() const;
  167. /// Dump the world into the log file.
  168. /// @warning this should be called outside of a time step.
  169. void Dump();
  170. private:
  171. // m_flags
  172. enum
  173. {
  174. e_newFixture = 0x0001,
  175. e_locked = 0x0002,
  176. e_clearForces = 0x0004
  177. };
  178. friend class b2Body;
  179. friend class b2Fixture;
  180. friend class b2ContactManager;
  181. friend class b2Controller;
  182. void Solve(const b2TimeStep& step);
  183. void SolveTOI(const b2TimeStep& step);
  184. void DrawJoint(b2Joint* joint);
  185. void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
  186. b2BlockAllocator m_blockAllocator;
  187. b2StackAllocator m_stackAllocator;
  188. int32 m_flags;
  189. b2ContactManager m_contactManager;
  190. b2Body* m_bodyList;
  191. b2Joint* m_jointList;
  192. int32 m_bodyCount;
  193. int32 m_jointCount;
  194. b2Vec2 m_gravity;
  195. bool m_allowSleep;
  196. b2DestructionListener* m_destructionListener;
  197. b2Draw* m_debugDraw;
  198. // This is used to compute the time step ratio to
  199. // support a variable time step.
  200. float32 m_inv_dt0;
  201. // These are for debugging the solver.
  202. bool m_warmStarting;
  203. bool m_continuousPhysics;
  204. bool m_subStepping;
  205. bool m_stepComplete;
  206. b2Profile m_profile;
  207. };
  208. inline b2Body* b2World::GetBodyList()
  209. {
  210. return m_bodyList;
  211. }
  212. inline const b2Body* b2World::GetBodyList() const
  213. {
  214. return m_bodyList;
  215. }
  216. inline b2Joint* b2World::GetJointList()
  217. {
  218. return m_jointList;
  219. }
  220. inline const b2Joint* b2World::GetJointList() const
  221. {
  222. return m_jointList;
  223. }
  224. inline b2Contact* b2World::GetContactList()
  225. {
  226. return m_contactManager.m_contactList;
  227. }
  228. inline const b2Contact* b2World::GetContactList() const
  229. {
  230. return m_contactManager.m_contactList;
  231. }
  232. inline int32 b2World::GetBodyCount() const
  233. {
  234. return m_bodyCount;
  235. }
  236. inline int32 b2World::GetJointCount() const
  237. {
  238. return m_jointCount;
  239. }
  240. inline int32 b2World::GetContactCount() const
  241. {
  242. return m_contactManager.m_contactCount;
  243. }
  244. inline void b2World::SetGravity(const b2Vec2& gravity)
  245. {
  246. m_gravity = gravity;
  247. }
  248. inline b2Vec2 b2World::GetGravity() const
  249. {
  250. return m_gravity;
  251. }
  252. inline bool b2World::IsLocked() const
  253. {
  254. return (m_flags & e_locked) == e_locked;
  255. }
  256. inline void b2World::SetAutoClearForces(bool flag)
  257. {
  258. if (flag)
  259. {
  260. m_flags |= e_clearForces;
  261. }
  262. else
  263. {
  264. m_flags &= ~e_clearForces;
  265. }
  266. }
  267. /// Get the flag that controls automatic clearing of forces after each time step.
  268. inline bool b2World::GetAutoClearForces() const
  269. {
  270. return (m_flags & e_clearForces) == e_clearForces;
  271. }
  272. inline const b2ContactManager& b2World::GetContactManager() const
  273. {
  274. return m_contactManager;
  275. }
  276. inline const b2Profile& b2World::GetProfile() const
  277. {
  278. return m_profile;
  279. }
  280. #endif