b2World.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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 "../Common/b2Math.h"
  21. #include "../Common/b2BlockAllocator.h"
  22. #include "../Common/b2StackAllocator.h"
  23. #include "b2ContactManager.h"
  24. #include "b2WorldCallbacks.h"
  25. struct b2AABB;
  26. struct b2ShapeDef;
  27. struct b2BodyDef;
  28. struct b2JointDef;
  29. class b2Body;
  30. class b2Joint;
  31. class b2Shape;
  32. class b2Contact;
  33. class b2BroadPhase;
  34. class b2Controller;
  35. class b2ControllerDef;
  36. struct b2TimeStep
  37. {
  38. float32 dt; // time step
  39. float32 inv_dt; // inverse time step (0 if dt == 0).
  40. float32 dtRatio; // dt * inv_dt0
  41. int32 velocityIterations;
  42. int32 positionIterations;
  43. bool warmStarting;
  44. };
  45. /// The world class manages all physics entities, dynamic simulation,
  46. /// and asynchronous queries. The world also contains efficient memory
  47. /// management facilities.
  48. class b2World
  49. {
  50. public:
  51. /// Construct a world object.
  52. /// @param worldAABB a bounding box that completely encompasses all your shapes.
  53. /// @param gravity the world gravity vector.
  54. /// @param doSleep improve performance by not simulating inactive bodies.
  55. b2World(const b2AABB& worldAABB, const b2Vec2& gravity, bool doSleep);
  56. /// Destruct the world. All physics entities are destroyed and all heap memory is released.
  57. ~b2World();
  58. /// Register a destruction listener.
  59. void SetDestructionListener(b2DestructionListener* listener);
  60. /// Register a broad-phase boundary listener.
  61. void SetBoundaryListener(b2BoundaryListener* listener);
  62. /// Register a contact filter to provide specific control over collision.
  63. /// Otherwise the default filter is used (b2_defaultFilter).
  64. void SetContactFilter(b2ContactFilter* filter);
  65. /// Register a contact event listener
  66. void SetContactListener(b2ContactListener* listener);
  67. /// Register a routine for debug drawing. The debug draw functions are called
  68. /// inside the b2World::Step method, so make sure your renderer is ready to
  69. /// consume draw commands when you call Step().
  70. void SetDebugDraw(b2DebugDraw* debugDraw);
  71. /// Create a rigid body given a definition. No reference to the definition
  72. /// is retained.
  73. /// @warning This function is locked during callbacks.
  74. b2Body* CreateBody(const b2BodyDef* def);
  75. /// Destroy a rigid body given a definition. No reference to the definition
  76. /// is retained. This function is locked during callbacks.
  77. /// @warning This automatically deletes all associated shapes and joints.
  78. /// @warning This function is locked during callbacks.
  79. void DestroyBody(b2Body* body);
  80. /// Create a joint to constrain bodies together. No reference to the definition
  81. /// is retained. This may cause the connected bodies to cease colliding.
  82. /// @warning This function is locked during callbacks.
  83. b2Joint* CreateJoint(const b2JointDef* def);
  84. /// Destroy a joint. This may cause the connected bodies to begin colliding.
  85. /// @warning This function is locked during callbacks.
  86. void DestroyJoint(b2Joint* joint);
  87. /// Add a controller to the world.
  88. b2Controller* CreateController(b2ControllerDef* def);
  89. /// Removes a controller from the world.
  90. void DestroyController(b2Controller* controller);
  91. /// The world provides a single static ground body with no collision shapes.
  92. /// You can use this to simplify the creation of joints and static shapes.
  93. b2Body* GetGroundBody();
  94. /// Take a time step. This performs collision detection, integration,
  95. /// and constraint solution.
  96. /// @param timeStep the amount of time to simulate, this should not vary.
  97. /// @param velocityIterations for the velocity constraint solver.
  98. /// @param positionIterations for the position constraint solver.
  99. void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations);
  100. /// Query the world for all shapes that potentially overlap the
  101. /// provided AABB. You provide a shape pointer buffer of specified
  102. /// size. The number of shapes found is returned.
  103. /// @param aabb the query box.
  104. /// @param shapes a user allocated shape pointer array of size maxCount (or greater).
  105. /// @param maxCount the capacity of the shapes array.
  106. /// @return the number of shapes found in aabb.
  107. int32 Query(const b2AABB& aabb, b2Shape** shapes, int32 maxCount);
  108. /// Query the world for all shapes that intersect a given segment. You provide a shap
  109. /// pointer buffer of specified size. The number of shapes found is returned, and the buffer
  110. /// is filled in order of intersection
  111. /// @param segment defines the begin and end point of the ray cast, from p1 to p2.
  112. /// Use b2Segment.Extend to create (semi-)infinite rays
  113. /// @param shapes a user allocated shape pointer array of size maxCount (or greater).
  114. /// @param maxCount the capacity of the shapes array
  115. /// @param solidShapes determines if shapes that the ray starts in are counted as hits.
  116. /// @param userData passed through the worlds contact filter, with method RayCollide. This can be used to filter valid shapes
  117. /// @returns the number of shapes found
  118. int32 Raycast(const b2Segment& segment, b2Shape** shapes, int32 maxCount, bool solidShapes, void* userData);
  119. /// Performs a raycast as with Raycast, finding the first intersecting shape.
  120. /// @param segment defines the begin and end point of the ray cast, from p1 to p2.
  121. /// Use b2Segment.Extend to create (semi-)infinite rays
  122. /// @param lambda returns the hit fraction. You can use this to compute the contact point
  123. /// p = (1 - lambda) * segment.p1 + lambda * segment.p2.
  124. /// @param normal returns the normal at the contact point. If there is no intersection, the normal
  125. /// is not set.
  126. /// @param solidShapes determines if shapes that the ray starts in are counted as hits.
  127. /// @returns the colliding shape shape, or null if not found
  128. b2Shape* RaycastOne(const b2Segment& segment, float32* lambda, b2Vec2* normal, bool solidShapes, void* userData);
  129. /// Check if the AABB is within the broadphase limits.
  130. bool InRange(const b2AABB& aabb) const;
  131. /// Get the world body list. With the returned body, use b2Body::GetNext to get
  132. /// the next body in the world list. A NULL body indicates the end of the list.
  133. /// @return the head of the world body list.
  134. b2Body* GetBodyList();
  135. /// Get the world joint list. With the returned joint, use b2Joint::GetNext to get
  136. /// the next joint in the world list. A NULL joint indicates the end of the list.
  137. /// @return the head of the world joint list.
  138. b2Joint* GetJointList();
  139. /// Get the world controller list. With the returned controller, use b2Controller::GetNext to get
  140. /// the next controller in the world list. A NULL controller indicates the end of the list.
  141. /// @return the head of the world controller list.
  142. b2Controller* GetControllerList();
  143. /// Re-filter a shape. This re-runs contact filtering on a shape.
  144. void Refilter(b2Shape* shape);
  145. /// Enable/disable warm starting. For testing.
  146. void SetWarmStarting(bool flag) { m_warmStarting = flag; }
  147. /// Enable/disable continuous physics. For testing.
  148. void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
  149. /// Perform validation of internal data structures.
  150. void Validate();
  151. /// Get the number of broad-phase proxies.
  152. int32 GetProxyCount() const;
  153. /// Get the number of broad-phase pairs.
  154. int32 GetPairCount() const;
  155. /// Get the number of bodies.
  156. int32 GetBodyCount() const;
  157. /// Get the number of joints.
  158. int32 GetJointCount() const;
  159. /// Get the number of contacts (each may have 0 or more contact points).
  160. int32 GetContactCount() const;
  161. /// Get the number of controllers.
  162. int32 GetControllerCount() const;
  163. /// Change the global gravity vector.
  164. void SetGravity(const b2Vec2& gravity);
  165. /// Get the global gravity vector.
  166. b2Vec2 GetGravity() const;
  167. private:
  168. friend class b2Body;
  169. friend class b2ContactManager;
  170. friend class b2Controller;
  171. void Solve(const b2TimeStep& step);
  172. void SolveTOI(const b2TimeStep& step);
  173. void DrawJoint(b2Joint* joint);
  174. void DrawShape(b2Shape* shape, const b2XForm& xf, const b2Color& color, bool core);
  175. void DrawDebugData();
  176. //Is it safe to pass private static function pointers?
  177. static float32 RaycastSortKey(void* shape);
  178. b2BlockAllocator m_blockAllocator;
  179. b2StackAllocator m_stackAllocator;
  180. bool m_lock;
  181. b2BroadPhase* m_broadPhase;
  182. b2ContactManager m_contactManager;
  183. b2Body* m_bodyList;
  184. b2Joint* m_jointList;
  185. b2Controller* m_controllerList;
  186. b2Vec2 m_raycastNormal;
  187. void* m_raycastUserData;
  188. const b2Segment* m_raycastSegment;
  189. bool m_raycastSolidShape;
  190. // Do not access
  191. b2Contact* m_contactList;
  192. int32 m_bodyCount;
  193. int32 m_contactCount;
  194. int32 m_jointCount;
  195. int32 m_controllerCount;
  196. b2Vec2 m_gravity;
  197. bool m_allowSleep;
  198. b2Body* m_groundBody;
  199. b2DestructionListener* m_destructionListener;
  200. b2BoundaryListener* m_boundaryListener;
  201. b2ContactFilter* m_contactFilter;
  202. b2ContactListener* m_contactListener;
  203. b2DebugDraw* m_debugDraw;
  204. // This is used to compute the time step ratio to
  205. // support a variable time step.
  206. float32 m_inv_dt0;
  207. // This is for debugging the solver.
  208. bool m_warmStarting;
  209. // This is for debugging the solver.
  210. bool m_continuousPhysics;
  211. };
  212. inline b2Body* b2World::GetGroundBody()
  213. {
  214. return m_groundBody;
  215. }
  216. inline b2Body* b2World::GetBodyList()
  217. {
  218. return m_bodyList;
  219. }
  220. inline b2Joint* b2World::GetJointList()
  221. {
  222. return m_jointList;
  223. }
  224. inline b2Controller* b2World::GetControllerList()
  225. {
  226. return m_controllerList;
  227. }
  228. inline int32 b2World::GetBodyCount() const
  229. {
  230. return m_bodyCount;
  231. }
  232. inline int32 b2World::GetJointCount() const
  233. {
  234. return m_jointCount;
  235. }
  236. inline int32 b2World::GetContactCount() const
  237. {
  238. return m_contactCount;
  239. }
  240. inline int32 b2World::GetControllerCount() const
  241. {
  242. return m_controllerCount;
  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. #endif