btDiscreteDynamicsWorld.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_DISCRETE_DYNAMICS_WORLD_H
  14. #define BT_DISCRETE_DYNAMICS_WORLD_H
  15. #include "btDynamicsWorld.h"
  16. class btDispatcher;
  17. class btOverlappingPairCache;
  18. class btConstraintSolver;
  19. class btSimulationIslandManager;
  20. class btTypedConstraint;
  21. class btActionInterface;
  22. class btPersistentManifold;
  23. class btIDebugDraw;
  24. struct InplaceSolverIslandCallback;
  25. #include "LinearMath/btAlignedObjectArray.h"
  26. #include "LinearMath/btThreads.h"
  27. ///btDiscreteDynamicsWorld provides discrete rigid body simulation
  28. ///those classes replace the obsolete CcdPhysicsEnvironment/CcdPhysicsController
  29. ATTRIBUTE_ALIGNED16(class)
  30. btDiscreteDynamicsWorld : public btDynamicsWorld
  31. {
  32. protected:
  33. btAlignedObjectArray<btTypedConstraint*> m_sortedConstraints;
  34. InplaceSolverIslandCallback* m_solverIslandCallback;
  35. btConstraintSolver* m_constraintSolver;
  36. btSimulationIslandManager* m_islandManager;
  37. btAlignedObjectArray<btTypedConstraint*> m_constraints;
  38. btAlignedObjectArray<btRigidBody*> m_nonStaticRigidBodies;
  39. btVector3 m_gravity;
  40. //for variable timesteps
  41. btScalar m_localTime;
  42. btScalar m_fixedTimeStep;
  43. //for variable timesteps
  44. bool m_ownsIslandManager;
  45. bool m_ownsConstraintSolver;
  46. bool m_synchronizeAllMotionStates;
  47. bool m_applySpeculativeContactRestitution;
  48. btAlignedObjectArray<btActionInterface*> m_actions;
  49. int m_profileTimings;
  50. bool m_latencyMotionStateInterpolation;
  51. btAlignedObjectArray<btPersistentManifold*> m_predictiveManifolds;
  52. btSpinMutex m_predictiveManifoldsMutex; // used to synchronize threads creating predictive contacts
  53. virtual void predictUnconstraintMotion(btScalar timeStep);
  54. void integrateTransformsInternal(btRigidBody * *bodies, int numBodies, btScalar timeStep); // can be called in parallel
  55. virtual void integrateTransforms(btScalar timeStep);
  56. virtual void calculateSimulationIslands();
  57. virtual void updateActivationState(btScalar timeStep);
  58. void updateActions(btScalar timeStep);
  59. void startProfiling(btScalar timeStep);
  60. virtual void internalSingleStepSimulation(btScalar timeStep);
  61. void releasePredictiveContacts();
  62. void createPredictiveContactsInternal(btRigidBody * *bodies, int numBodies, btScalar timeStep); // can be called in parallel
  63. virtual void createPredictiveContacts(btScalar timeStep);
  64. virtual void saveKinematicState(btScalar timeStep);
  65. void serializeRigidBodies(btSerializer * serializer);
  66. void serializeDynamicsWorldInfo(btSerializer * serializer);
  67. public:
  68. BT_DECLARE_ALIGNED_ALLOCATOR();
  69. ///this btDiscreteDynamicsWorld constructor gets created objects from the user, and will not delete those
  70. btDiscreteDynamicsWorld(btDispatcher * dispatcher, btBroadphaseInterface * pairCache, btConstraintSolver * constraintSolver, btCollisionConfiguration * collisionConfiguration);
  71. virtual ~btDiscreteDynamicsWorld();
  72. ///if maxSubSteps > 0, it will interpolate motion between fixedTimeStep's
  73. virtual int stepSimulation(btScalar timeStep, int maxSubSteps = 1, btScalar fixedTimeStep = btScalar(1.) / btScalar(60.));
  74. virtual void solveConstraints(btContactSolverInfo & solverInfo);
  75. virtual void synchronizeMotionStates();
  76. ///this can be useful to synchronize a single rigid body -> graphics object
  77. void synchronizeSingleMotionState(btRigidBody * body);
  78. virtual void addConstraint(btTypedConstraint * constraint, bool disableCollisionsBetweenLinkedBodies = false);
  79. virtual void removeConstraint(btTypedConstraint * constraint);
  80. virtual void addAction(btActionInterface*);
  81. virtual void removeAction(btActionInterface*);
  82. btSimulationIslandManager* getSimulationIslandManager()
  83. {
  84. return m_islandManager;
  85. }
  86. const btSimulationIslandManager* getSimulationIslandManager() const
  87. {
  88. return m_islandManager;
  89. }
  90. btCollisionWorld* getCollisionWorld()
  91. {
  92. return this;
  93. }
  94. virtual void setGravity(const btVector3& gravity);
  95. virtual btVector3 getGravity() const;
  96. virtual void addCollisionObject(btCollisionObject * collisionObject, int collisionFilterGroup = btBroadphaseProxy::StaticFilter, int collisionFilterMask = btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
  97. virtual void addRigidBody(btRigidBody * body);
  98. virtual void addRigidBody(btRigidBody * body, int group, int mask);
  99. virtual void removeRigidBody(btRigidBody * body);
  100. ///removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise call btCollisionWorld::removeCollisionObject
  101. virtual void removeCollisionObject(btCollisionObject * collisionObject);
  102. virtual void debugDrawConstraint(btTypedConstraint * constraint);
  103. virtual void debugDrawWorld();
  104. virtual void setConstraintSolver(btConstraintSolver * solver);
  105. virtual btConstraintSolver* getConstraintSolver();
  106. virtual int getNumConstraints() const;
  107. virtual btTypedConstraint* getConstraint(int index);
  108. virtual const btTypedConstraint* getConstraint(int index) const;
  109. virtual btDynamicsWorldType getWorldType() const
  110. {
  111. return BT_DISCRETE_DYNAMICS_WORLD;
  112. }
  113. ///the forces on each rigidbody is accumulating together with gravity. clear this after each timestep.
  114. virtual void clearForces();
  115. ///apply gravity, call this once per timestep
  116. virtual void applyGravity();
  117. virtual void setNumTasks(int numTasks)
  118. {
  119. (void)numTasks;
  120. }
  121. ///obsolete, use updateActions instead
  122. virtual void updateVehicles(btScalar timeStep)
  123. {
  124. updateActions(timeStep);
  125. }
  126. ///obsolete, use addAction instead
  127. virtual void addVehicle(btActionInterface * vehicle);
  128. ///obsolete, use removeAction instead
  129. virtual void removeVehicle(btActionInterface * vehicle);
  130. ///obsolete, use addAction instead
  131. virtual void addCharacter(btActionInterface * character);
  132. ///obsolete, use removeAction instead
  133. virtual void removeCharacter(btActionInterface * character);
  134. void setSynchronizeAllMotionStates(bool synchronizeAll)
  135. {
  136. m_synchronizeAllMotionStates = synchronizeAll;
  137. }
  138. bool getSynchronizeAllMotionStates() const
  139. {
  140. return m_synchronizeAllMotionStates;
  141. }
  142. void setApplySpeculativeContactRestitution(bool enable)
  143. {
  144. m_applySpeculativeContactRestitution = enable;
  145. }
  146. bool getApplySpeculativeContactRestitution() const
  147. {
  148. return m_applySpeculativeContactRestitution;
  149. }
  150. ///Preliminary serialization test for Bullet 2.76. Loading those files requires a separate parser (see Bullet/Demos/SerializeDemo)
  151. virtual void serialize(btSerializer * serializer);
  152. ///Interpolate motion state between previous and current transform, instead of current and next transform.
  153. ///This can relieve discontinuities in the rendering, due to penetrations
  154. void setLatencyMotionStateInterpolation(bool latencyInterpolation)
  155. {
  156. m_latencyMotionStateInterpolation = latencyInterpolation;
  157. }
  158. bool getLatencyMotionStateInterpolation() const
  159. {
  160. return m_latencyMotionStateInterpolation;
  161. }
  162. btAlignedObjectArray<btRigidBody*>& getNonStaticRigidBodies()
  163. {
  164. return m_nonStaticRigidBodies;
  165. }
  166. const btAlignedObjectArray<btRigidBody*>& getNonStaticRigidBodies() const
  167. {
  168. return m_nonStaticRigidBodies;
  169. }
  170. };
  171. #endif //BT_DISCRETE_DYNAMICS_WORLD_H