Upgrade Bullet to version 2.83.
[blender.git] / extern / bullet2 / src / BulletDynamics / Dynamics / btDiscreteDynamicsWorld.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages 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 freely, 
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17 #ifndef BT_DISCRETE_DYNAMICS_WORLD_H
18 #define BT_DISCRETE_DYNAMICS_WORLD_H
19
20 #include "btDynamicsWorld.h"
21
22 class btDispatcher;
23 class btOverlappingPairCache;
24 class btConstraintSolver;
25 class btSimulationIslandManager;
26 class btTypedConstraint;
27 class btActionInterface;
28 class btPersistentManifold;
29 class btIDebugDraw;
30 struct InplaceSolverIslandCallback;
31
32 #include "LinearMath/btAlignedObjectArray.h"
33
34
35 ///btDiscreteDynamicsWorld provides discrete rigid body simulation
36 ///those classes replace the obsolete CcdPhysicsEnvironment/CcdPhysicsController
37 ATTRIBUTE_ALIGNED16(class) btDiscreteDynamicsWorld : public btDynamicsWorld
38 {
39 protected:
40         
41     btAlignedObjectArray<btTypedConstraint*>    m_sortedConstraints;
42         InplaceSolverIslandCallback*    m_solverIslandCallback;
43
44         btConstraintSolver*     m_constraintSolver;
45
46         btSimulationIslandManager*      m_islandManager;
47
48         btAlignedObjectArray<btTypedConstraint*> m_constraints;
49
50         btAlignedObjectArray<btRigidBody*> m_nonStaticRigidBodies;
51
52         btVector3       m_gravity;
53
54         //for variable timesteps
55         btScalar        m_localTime;
56         btScalar        m_fixedTimeStep;
57         //for variable timesteps
58
59         bool    m_ownsIslandManager;
60         bool    m_ownsConstraintSolver;
61         bool    m_synchronizeAllMotionStates;
62         bool    m_applySpeculativeContactRestitution;
63
64         btAlignedObjectArray<btActionInterface*>        m_actions;
65         
66         int     m_profileTimings;
67
68         bool    m_latencyMotionStateInterpolation;
69
70         btAlignedObjectArray<btPersistentManifold*>     m_predictiveManifolds;
71
72         virtual void    predictUnconstraintMotion(btScalar timeStep);
73         
74         virtual void    integrateTransforms(btScalar timeStep);
75                 
76         virtual void    calculateSimulationIslands();
77
78         virtual void    solveConstraints(btContactSolverInfo& solverInfo);
79         
80         virtual void    updateActivationState(btScalar timeStep);
81
82         void    updateActions(btScalar timeStep);
83
84         void    startProfiling(btScalar timeStep);
85
86         virtual void    internalSingleStepSimulation( btScalar timeStep);
87
88         void    createPredictiveContacts(btScalar timeStep);
89
90         virtual void    saveKinematicState(btScalar timeStep);
91
92         void    serializeRigidBodies(btSerializer* serializer);
93
94         void    serializeDynamicsWorldInfo(btSerializer* serializer);
95
96 public:
97
98
99         BT_DECLARE_ALIGNED_ALLOCATOR();
100
101         ///this btDiscreteDynamicsWorld constructor gets created objects from the user, and will not delete those
102         btDiscreteDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration);
103
104         virtual ~btDiscreteDynamicsWorld();
105
106         ///if maxSubSteps > 0, it will interpolate motion between fixedTimeStep's
107         virtual int     stepSimulation( btScalar timeStep,int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.));
108
109
110         virtual void    synchronizeMotionStates();
111
112         ///this can be useful to synchronize a single rigid body -> graphics object
113         void    synchronizeSingleMotionState(btRigidBody* body);
114
115         virtual void    addConstraint(btTypedConstraint* constraint, bool disableCollisionsBetweenLinkedBodies=false);
116
117         virtual void    removeConstraint(btTypedConstraint* constraint);
118
119         virtual void    addAction(btActionInterface*);
120
121         virtual void    removeAction(btActionInterface*);
122         
123         btSimulationIslandManager*      getSimulationIslandManager()
124         {
125                 return m_islandManager;
126         }
127
128         const btSimulationIslandManager*        getSimulationIslandManager() const 
129         {
130                 return m_islandManager;
131         }
132
133         btCollisionWorld*       getCollisionWorld()
134         {
135                 return this;
136         }
137
138         virtual void    setGravity(const btVector3& gravity);
139
140         virtual btVector3 getGravity () const;
141
142         virtual void    addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::StaticFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
143
144         virtual void    addRigidBody(btRigidBody* body);
145
146         virtual void    addRigidBody(btRigidBody* body, short group, short mask);
147
148         virtual void    removeRigidBody(btRigidBody* body);
149
150         ///removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise call btCollisionWorld::removeCollisionObject
151         virtual void    removeCollisionObject(btCollisionObject* collisionObject);
152
153
154         virtual void    debugDrawConstraint(btTypedConstraint* constraint);
155
156         virtual void    debugDrawWorld();
157
158         virtual void    setConstraintSolver(btConstraintSolver* solver);
159
160         virtual btConstraintSolver* getConstraintSolver();
161         
162         virtual int             getNumConstraints() const;
163
164         virtual btTypedConstraint* getConstraint(int index)     ;
165
166         virtual const btTypedConstraint* getConstraint(int index) const;
167
168         
169         virtual btDynamicsWorldType     getWorldType() const
170         {
171                 return BT_DISCRETE_DYNAMICS_WORLD;
172         }
173         
174         ///the forces on each rigidbody is accumulating together with gravity. clear this after each timestep.
175         virtual void    clearForces();
176
177         ///apply gravity, call this once per timestep
178         virtual void    applyGravity();
179
180         virtual void    setNumTasks(int numTasks)
181         {
182         (void) numTasks;
183         }
184
185         ///obsolete, use updateActions instead
186         virtual void updateVehicles(btScalar timeStep)
187         {
188                 updateActions(timeStep);
189         }
190
191         ///obsolete, use addAction instead
192         virtual void    addVehicle(btActionInterface* vehicle);
193         ///obsolete, use removeAction instead
194         virtual void    removeVehicle(btActionInterface* vehicle);
195         ///obsolete, use addAction instead
196         virtual void    addCharacter(btActionInterface* character);
197         ///obsolete, use removeAction instead
198         virtual void    removeCharacter(btActionInterface* character);
199
200         void    setSynchronizeAllMotionStates(bool synchronizeAll)
201         {
202                 m_synchronizeAllMotionStates = synchronizeAll;
203         }
204         bool getSynchronizeAllMotionStates() const
205         {
206                 return m_synchronizeAllMotionStates;
207         }
208
209         void setApplySpeculativeContactRestitution(bool enable)
210         {
211                 m_applySpeculativeContactRestitution = enable;
212         }
213         
214         bool getApplySpeculativeContactRestitution() const
215         {
216                 return m_applySpeculativeContactRestitution;
217         }
218
219         ///Preliminary serialization test for Bullet 2.76. Loading those files requires a separate parser (see Bullet/Demos/SerializeDemo)
220         virtual void    serialize(btSerializer* serializer);
221
222         ///Interpolate motion state between previous and current transform, instead of current and next transform.
223         ///This can relieve discontinuities in the rendering, due to penetrations
224         void setLatencyMotionStateInterpolation(bool latencyInterpolation )
225         {
226                 m_latencyMotionStateInterpolation = latencyInterpolation;
227         }
228         bool getLatencyMotionStateInterpolation() const
229         {
230                 return m_latencyMotionStateInterpolation;
231         }
232 };
233
234 #endif //BT_DISCRETE_DYNAMICS_WORLD_H