b2_world.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_WORLD_H
  19. #define B2_WORLD_H
  20. #include "b2_api.h"
  21. #include "b2_block_allocator.h"
  22. #include "b2_contact_manager.h"
  23. #include "b2_math.h"
  24. #include "b2_stack_allocator.h"
  25. #include "b2_time_step.h"
  26. #include "b2_world_callbacks.h"
  27. struct b2AABB;
  28. struct b2BodyDef;
  29. struct b2Color;
  30. struct b2JointDef;
  31. class b2Body;
  32. class b2Draw;
  33. class b2Fixture;
  34. class b2Joint;
  35. /// The world class manages all physics entities, dynamic simulation,
  36. /// and asynchronous queries. The world also contains efficient memory
  37. /// management facilities.
  38. class B2_API b2World
  39. {
  40. public:
  41. /// Construct a world object.
  42. /// @param gravity the world gravity vector.
  43. b2World(const b2Vec2& gravity);
  44. /// Destruct the world. All physics entities are destroyed and all heap memory is released.
  45. ~b2World();
  46. /// Register a destruction listener. The listener is owned by you and must
  47. /// remain in scope.
  48. void SetDestructionListener(b2DestructionListener* listener);
  49. /// Register a contact filter to provide specific control over collision.
  50. /// Otherwise the default filter is used (b2_defaultFilter). The listener is
  51. /// owned by you and must remain in scope.
  52. void SetContactFilter(b2ContactFilter* filter);
  53. /// Register a contact event listener. The listener is owned by you and must
  54. /// remain in scope.
  55. void SetContactListener(b2ContactListener* listener);
  56. /// Register a routine for debug drawing. The debug draw functions are called
  57. /// inside with b2World::DebugDraw method. The debug draw object is owned
  58. /// by you and must remain in scope.
  59. void SetDebugDraw(b2Draw* debugDraw);
  60. /// Create a rigid body given a definition. No reference to the definition
  61. /// is retained.
  62. /// @warning This function is locked during callbacks.
  63. b2Body* CreateBody(const b2BodyDef* def);
  64. /// Destroy a rigid body given a definition. No reference to the definition
  65. /// is retained. This function is locked during callbacks.
  66. /// @warning This automatically deletes all associated shapes and joints.
  67. /// @warning This function is locked during callbacks.
  68. void DestroyBody(b2Body* body);
  69. /// Create a joint to constrain bodies together. No reference to the definition
  70. /// is retained. This may cause the connected bodies to cease colliding.
  71. /// @warning This function is locked during callbacks.
  72. b2Joint* CreateJoint(const b2JointDef* def);
  73. /// Destroy a joint. This may cause the connected bodies to begin colliding.
  74. /// @warning This function is locked during callbacks.
  75. void DestroyJoint(b2Joint* joint);
  76. /// Take a time step. This performs collision detection, integration,
  77. /// and constraint solution.
  78. /// @param timeStep the amount of time to simulate, this should not vary.
  79. /// @param velocityIterations for the velocity constraint solver.
  80. /// @param positionIterations for the position constraint solver.
  81. void Step( float timeStep,
  82. int32 velocityIterations,
  83. int32 positionIterations);
  84. /// Manually clear the force buffer on all bodies. By default, forces are cleared automatically
  85. /// after each call to Step. The default behavior is modified by calling SetAutoClearForces.
  86. /// The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain
  87. /// a fixed sized time step under a variable frame-rate.
  88. /// When you perform sub-stepping you will disable auto clearing of forces and instead call
  89. /// ClearForces after all sub-steps are complete in one pass of your game loop.
  90. /// @see SetAutoClearForces
  91. void ClearForces();
  92. /// Call this to draw shapes and other debug draw data. This is intentionally non-const.
  93. void DebugDraw();
  94. /// Query the world for all fixtures that potentially overlap the
  95. /// provided AABB.
  96. /// @param callback a user implemented callback class.
  97. /// @param aabb the query box.
  98. void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
  99. /// Ray-cast the world for all fixtures in the path of the ray. Your callback
  100. /// controls whether you get the closest point, any point, or n-points.
  101. /// The ray-cast ignores shapes that contain the starting point.
  102. /// @param callback a user implemented callback class.
  103. /// @param point1 the ray starting point
  104. /// @param point2 the ray ending point
  105. void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
  106. /// Get the world body list. With the returned body, use b2Body::GetNext to get
  107. /// the next body in the world list. A nullptr body indicates the end of the list.
  108. /// @return the head of the world body list.
  109. b2Body* GetBodyList();
  110. const b2Body* GetBodyList() const;
  111. /// Get the world joint list. With the returned joint, use b2Joint::GetNext to get
  112. /// the next joint in the world list. A nullptr joint indicates the end of the list.
  113. /// @return the head of the world joint list.
  114. b2Joint* GetJointList();
  115. const b2Joint* GetJointList() const;
  116. /// Get the world contact list. With the returned contact, use b2Contact::GetNext to get
  117. /// the next contact in the world list. A nullptr contact indicates the end of the list.
  118. /// @return the head of the world contact list.
  119. /// @warning contacts are created and destroyed in the middle of a time step.
  120. /// Use b2ContactListener to avoid missing contacts.
  121. b2Contact* GetContactList();
  122. const b2Contact* GetContactList() const;
  123. /// Enable/disable sleep.
  124. void SetAllowSleeping(bool flag);
  125. bool GetAllowSleeping() const { return m_allowSleep; }
  126. /// Enable/disable warm starting. For testing.
  127. void SetWarmStarting(bool flag) { m_warmStarting = flag; }
  128. bool GetWarmStarting() const { return m_warmStarting; }
  129. /// Enable/disable continuous physics. For testing.
  130. void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
  131. bool GetContinuousPhysics() const { return m_continuousPhysics; }
  132. /// Enable/disable single stepped continuous physics. For testing.
  133. void SetSubStepping(bool flag) { m_subStepping = flag; }
  134. bool GetSubStepping() const { return m_subStepping; }
  135. /// Get the number of broad-phase proxies.
  136. int32 GetProxyCount() const;
  137. /// Get the number of bodies.
  138. int32 GetBodyCount() const;
  139. /// Get the number of joints.
  140. int32 GetJointCount() const;
  141. /// Get the number of contacts (each may have 0 or more contact points).
  142. int32 GetContactCount() const;
  143. /// Get the height of the dynamic tree.
  144. int32 GetTreeHeight() const;
  145. /// Get the balance of the dynamic tree.
  146. int32 GetTreeBalance() const;
  147. /// Get the quality metric of the dynamic tree. The smaller the better.
  148. /// The minimum is 1.
  149. float GetTreeQuality() const;
  150. /// Change the global gravity vector.
  151. void SetGravity(const b2Vec2& gravity);
  152. /// Get the global gravity vector.
  153. b2Vec2 GetGravity() const;
  154. /// Is the world locked (in the middle of a time step).
  155. bool IsLocked() const;
  156. /// Set flag to control automatic clearing of forces after each time step.
  157. void SetAutoClearForces(bool flag);
  158. /// Get the flag that controls automatic clearing of forces after each time step.
  159. bool GetAutoClearForces() const;
  160. /// Shift the world origin. Useful for large worlds.
  161. /// The body shift formula is: position -= newOrigin
  162. /// @param newOrigin the new origin with respect to the old origin
  163. void ShiftOrigin(const b2Vec2& newOrigin);
  164. /// Get the contact manager for testing.
  165. const b2ContactManager& GetContactManager() const;
  166. /// Get the current profile.
  167. const b2Profile& GetProfile() const;
  168. /// Dump the world into the log file.
  169. /// @warning this should be called outside of a time step.
  170. void Dump();
  171. private:
  172. friend class b2Body;
  173. friend class b2Fixture;
  174. friend class b2ContactManager;
  175. friend class b2Controller;
  176. void Solve(const b2TimeStep& step);
  177. void SolveTOI(const b2TimeStep& step);
  178. void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
  179. b2BlockAllocator m_blockAllocator;
  180. b2StackAllocator m_stackAllocator;
  181. b2ContactManager m_contactManager;
  182. b2Body* m_bodyList;
  183. b2Joint* m_jointList;
  184. int32 m_bodyCount;
  185. int32 m_jointCount;
  186. b2Vec2 m_gravity;
  187. bool m_allowSleep;
  188. b2DestructionListener* m_destructionListener;
  189. b2Draw* m_debugDraw;
  190. // This is used to compute the time step ratio to
  191. // support a variable time step.
  192. float m_inv_dt0;
  193. bool m_newContacts;
  194. bool m_locked;
  195. bool m_clearForces;
  196. // These are for debugging the solver.
  197. bool m_warmStarting;
  198. bool m_continuousPhysics;
  199. bool m_subStepping;
  200. bool m_stepComplete;
  201. b2Profile m_profile;
  202. };
  203. inline b2Body* b2World::GetBodyList()
  204. {
  205. return m_bodyList;
  206. }
  207. inline const b2Body* b2World::GetBodyList() const
  208. {
  209. return m_bodyList;
  210. }
  211. inline b2Joint* b2World::GetJointList()
  212. {
  213. return m_jointList;
  214. }
  215. inline const b2Joint* b2World::GetJointList() const
  216. {
  217. return m_jointList;
  218. }
  219. inline b2Contact* b2World::GetContactList()
  220. {
  221. return m_contactManager.m_contactList;
  222. }
  223. inline const b2Contact* b2World::GetContactList() const
  224. {
  225. return m_contactManager.m_contactList;
  226. }
  227. inline int32 b2World::GetBodyCount() const
  228. {
  229. return m_bodyCount;
  230. }
  231. inline int32 b2World::GetJointCount() const
  232. {
  233. return m_jointCount;
  234. }
  235. inline int32 b2World::GetContactCount() const
  236. {
  237. return m_contactManager.m_contactCount;
  238. }
  239. inline void b2World::SetGravity(const b2Vec2& gravity)
  240. {
  241. m_gravity = gravity;
  242. }
  243. inline b2Vec2 b2World::GetGravity() const
  244. {
  245. return m_gravity;
  246. }
  247. inline bool b2World::IsLocked() const
  248. {
  249. return m_locked;
  250. }
  251. inline void b2World::SetAutoClearForces(bool flag)
  252. {
  253. m_clearForces = flag;
  254. }
  255. /// Get the flag that controls automatic clearing of forces after each time step.
  256. inline bool b2World::GetAutoClearForces() const
  257. {
  258. return m_clearForces;
  259. }
  260. inline const b2ContactManager& b2World::GetContactManager() const
  261. {
  262. return m_contactManager;
  263. }
  264. inline const b2Profile& b2World::GetProfile() const
  265. {
  266. return m_profile;
  267. }
  268. #endif