The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
11KB

  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 "../Common/b2Math.h"
  21. #include "../Common/b2BlockAllocator.h"
  22. #include "../Common/b2StackAllocator.h"
  23. #include "b2ContactManager.h"
  24. #include "b2WorldCallbacks.h"
  25. #include "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. juce::int32 velocityIterations,
  82. juce::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.
  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. juce::int32 GetProxyCount() const;
  136. /// Get the number of bodies.
  137. juce::int32 GetBodyCount() const;
  138. /// Get the number of joints.
  139. juce::int32 GetJointCount() const;
  140. /// Get the number of contacts (each may have 0 or more contact points).
  141. juce::int32 GetContactCount() const;
  142. /// Get the height of the dynamic tree.
  143. juce::int32 GetTreeHeight() const;
  144. /// Get the balance of the dynamic tree.
  145. juce::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. /// Get the contact manager for testing.
  160. const b2ContactManager& GetContactManager() const;
  161. /// Get the current profile.
  162. const b2Profile& GetProfile() const;
  163. /// Dump the world into the log file.
  164. /// @warning this should be called outside of a time step.
  165. void Dump();
  166. private:
  167. // m_flags
  168. enum
  169. {
  170. e_newFixture = 0x0001,
  171. e_locked = 0x0002,
  172. e_clearForces = 0x0004
  173. };
  174. friend class b2Body;
  175. friend class b2Fixture;
  176. friend class b2ContactManager;
  177. friend class b2Controller;
  178. void Solve(const b2TimeStep& step);
  179. void SolveTOI(const b2TimeStep& step);
  180. void DrawJoint(b2Joint* joint);
  181. void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
  182. b2BlockAllocator m_blockAllocator;
  183. b2StackAllocator m_stackAllocator;
  184. juce::int32 m_flags;
  185. b2ContactManager m_contactManager;
  186. b2Body* m_bodyList;
  187. b2Joint* m_jointList;
  188. juce::int32 m_bodyCount;
  189. juce::int32 m_jointCount;
  190. b2Vec2 m_gravity;
  191. bool m_allowSleep;
  192. b2DestructionListener* m_destructionListener;
  193. b2Draw* m_debugDraw;
  194. // This is used to compute the time step ratio to
  195. // support a variable time step.
  196. float32 m_inv_dt0;
  197. // These are for debugging the solver.
  198. bool m_warmStarting;
  199. bool m_continuousPhysics;
  200. bool m_subStepping;
  201. bool m_stepComplete;
  202. b2Profile m_profile;
  203. };
  204. inline b2Body* b2World::GetBodyList()
  205. {
  206. return m_bodyList;
  207. }
  208. inline const b2Body* b2World::GetBodyList() const
  209. {
  210. return m_bodyList;
  211. }
  212. inline b2Joint* b2World::GetJointList()
  213. {
  214. return m_jointList;
  215. }
  216. inline const b2Joint* b2World::GetJointList() const
  217. {
  218. return m_jointList;
  219. }
  220. inline b2Contact* b2World::GetContactList()
  221. {
  222. return m_contactManager.m_contactList;
  223. }
  224. inline const b2Contact* b2World::GetContactList() const
  225. {
  226. return m_contactManager.m_contactList;
  227. }
  228. inline juce::int32 b2World::GetBodyCount() const
  229. {
  230. return m_bodyCount;
  231. }
  232. inline juce::int32 b2World::GetJointCount() const
  233. {
  234. return m_jointCount;
  235. }
  236. inline juce::int32 b2World::GetContactCount() const
  237. {
  238. return m_contactManager.m_contactCount;
  239. }
  240. inline void b2World::SetGravity(const b2Vec2& gravity)
  241. {
  242. m_gravity = gravity;
  243. }
  244. inline b2Vec2 b2World::GetGravity() const
  245. {
  246. return m_gravity;
  247. }
  248. inline bool b2World::IsLocked() const
  249. {
  250. return (m_flags & e_locked) == e_locked;
  251. }
  252. inline void b2World::SetAutoClearForces(bool flag)
  253. {
  254. if (flag)
  255. {
  256. m_flags |= e_clearForces;
  257. }
  258. else
  259. {
  260. m_flags &= ~e_clearForces;
  261. }
  262. }
  263. /// Get the flag that controls automatic clearing of forces after each time step.
  264. inline bool b2World::GetAutoClearForces() const
  265. {
  266. return (m_flags & e_clearForces) == e_clearForces;
  267. }
  268. inline const b2ContactManager& b2World::GetContactManager() const
  269. {
  270. return m_contactManager;
  271. }
  272. inline const b2Profile& b2World::GetProfile() const
  273. {
  274. return m_profile;
  275. }
  276. #endif