Created a KX_SoftBodyDeformer for real-time soft bodies.
[blender.git] / source / gameengine / Physics / Bullet / CcdPhysicsEnvironment.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
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 #ifndef CCDPHYSICSENVIRONMENT
17 #define CCDPHYSICSENVIRONMENT
18
19 #include "PHY_IPhysicsEnvironment.h"
20 #include <vector>
21 #include <set>
22 class CcdPhysicsController;
23 #include "LinearMath/btVector3.h"
24 #include "LinearMath/btTransform.h"
25
26
27
28
29 class btTypedConstraint;
30 class btSimulationIslandManager;
31 class btCollisionDispatcher;
32 class btDispatcher;
33 //#include "btBroadphaseInterface.h"
34
35 //switch on/off new vehicle support
36 #define NEW_BULLET_VEHICLE_SUPPORT 1
37
38 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
39
40 class WrapperVehicle;
41 class btPersistentManifold;
42 class btBroadphaseInterface;
43 class btOverlappingPairCache;
44 class btIDebugDraw;
45 class PHY_IVehicle;
46 class CcdOverlapFilterCallBack;
47
48 /// CcdPhysicsEnvironment is an experimental mainloop for physics simulation using optional continuous collision detection.
49 /// Physics Environment takes care of stepping the simulation and is a container for physics entities.
50 /// It stores rigidbodies,constraints, materials etc.
51 /// A derived class may be able to 'construct' entities by loading and/or converting
52 class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
53 {
54         friend class CcdOverlapFilterCallBack;
55         btVector3 m_gravity;
56
57 protected:
58         btIDebugDraw*   m_debugDrawer;
59         
60         class btDefaultCollisionConfiguration* m_collisionConfiguration;
61         class btBroadphaseInterface*                    m_broadphase;
62
63         //solver iterations
64         int     m_numIterations;
65         
66         //timestep subdivisions
67         int     m_numTimeSubSteps;
68
69
70         int     m_ccdMode;
71         int     m_solverType;
72         int     m_profileTimings;
73         bool m_enableSatCollisionDetection;
74
75         btContactSolverInfo     m_solverInfo;
76         
77
78         public:
79                 CcdPhysicsEnvironment(btDispatcher* dispatcher=0, btOverlappingPairCache* pairCache=0);
80
81                 virtual         ~CcdPhysicsEnvironment();
82
83                 /////////////////////////////////////
84                 //PHY_IPhysicsEnvironment interface
85                 /////////////////////////////////////
86
87                 /// Perform an integration step of duration 'timeStep'.
88
89                 virtual void setDebugDrawer(btIDebugDraw* debugDrawer);
90
91                 virtual void            setNumIterations(int numIter);
92                 virtual void            setNumTimeSubSteps(int numTimeSubSteps)
93                 {
94                         m_numTimeSubSteps = numTimeSubSteps;
95                 }
96                 virtual void            setDeactivationTime(float dTime);
97                 virtual void            setDeactivationLinearTreshold(float linTresh) ;
98                 virtual void            setDeactivationAngularTreshold(float angTresh) ;
99                 virtual void            setContactBreakingTreshold(float contactBreakingTreshold) ;
100                 virtual void            setCcdMode(int ccdMode);
101                 virtual void            setSolverType(int solverType);
102                 virtual void            setSolverSorConstant(float sor);
103                 virtual void            setSolverTau(float tau);
104                 virtual void            setSolverDamping(float damping);
105                 virtual void            setLinearAirDamping(float damping);
106                 virtual void            setUseEpa(bool epa) ;
107
108                 virtual void            beginFrame();
109                 virtual void            endFrame() {};
110                 /// Perform an integration step of duration 'timeStep'.
111                 virtual bool            proceedDeltaTime(double curTime,float timeStep);
112 //              virtual bool            proceedDeltaTimeOneStep(float timeStep);
113
114                 virtual void            setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep){};
115                 //returns 0.f if no fixed timestep is used
116
117                 virtual float           getFixedTimeStep(){ return 0.f;};
118
119                 virtual void            setDebugMode(int debugMode);
120
121                 virtual void            setGravity(float x,float y,float z);
122                 virtual void            getGravity(PHY__Vector3& grav);
123
124
125                 virtual int                     createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
126                         float pivotX,float pivotY,float pivotZ,
127                         float axisX,float axisY,float axisZ,
128                         float axis1X=0,float axis1Y=0,float axis1Z=0,
129                         float axis2X=0,float axis2Y=0,float axis2Z=0
130                         );
131
132
133                 //Following the COLLADA physics specification for constraints
134                 virtual int                     createUniversalD6Constraint(
135                 class PHY_IPhysicsController* ctrlRef,class PHY_IPhysicsController* ctrlOther,
136                         btTransform& localAttachmentFrameRef,
137                         btTransform& localAttachmentOther,
138                         const btVector3& linearMinLimits,
139                         const btVector3& linearMaxLimits,
140                         const btVector3& angularMinLimits,
141                         const btVector3& angularMaxLimits
142                         );
143
144                 virtual void    setConstraintParam(int constraintId,int param,float value,float value1);
145
146             virtual void                removeConstraint(int    constraintid);
147
148                 virtual float           getAppliedImpulse(int   constraintid);
149
150
151                 virtual void    CallbackTriggers();
152
153
154 #ifdef NEW_BULLET_VEHICLE_SUPPORT
155                 //complex constraint for vehicles
156                 virtual PHY_IVehicle*   getVehicleConstraint(int constraintId);
157 #else
158                 virtual class PHY_IVehicle*     getVehicleConstraint(int constraintId)
159                 {
160                         return 0;
161                 }
162 #endif //NEW_BULLET_VEHICLE_SUPPORT
163
164                 btTypedConstraint*      getConstraintById(int constraintId);
165
166                 virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
167
168
169                 //Methods for gamelogic collision/physics callbacks
170                 virtual void addSensor(PHY_IPhysicsController* ctrl);
171                 virtual void removeSensor(PHY_IPhysicsController* ctrl);
172                 virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
173                 virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl);
174                 virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl);
175                 //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else
176                 virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
177                 virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
178         
179
180                 virtual int     getNumContactPoints();
181
182                 virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
183
184                 //////////////////////
185                 //CcdPhysicsEnvironment interface
186                 ////////////////////////
187         
188                 void    addCcdPhysicsController(CcdPhysicsController* ctrl);
189
190                 void    removeCcdPhysicsController(CcdPhysicsController* ctrl);
191
192                 void    updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask);
193
194                 void    disableCcdPhysicsController(CcdPhysicsController* ctrl);
195
196                 void    enableCcdPhysicsController(CcdPhysicsController* ctrl);
197
198                 btBroadphaseInterface*  getBroadphase();
199
200                 btDispatcher*   getDispatcher();
201                 
202
203                 bool    IsSatCollisionDetectionEnabled() const
204                 {
205                         return m_enableSatCollisionDetection;
206                 }
207
208                 void    EnableSatCollisionDetection(bool enableSat)
209                 {
210                         m_enableSatCollisionDetection = enableSat;
211                 }
212
213         
214                 const btPersistentManifold*     GetManifold(int index) const;
215
216         
217                 void    SyncMotionStates(float timeStep);
218
219                 class   btSoftRigidDynamicsWorld*       getDynamicsWorld()
220                 {
221                         return m_dynamicsWorld;
222                 }
223         
224                 class btConstraintSolver*       GetConstraintSolver();
225
226         protected:
227                 
228                 
229
230                 
231                 std::set<CcdPhysicsController*> m_controllers;
232                 
233                 std::set<CcdPhysicsController*> m_triggerControllers;
234
235                 PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
236                 void*                   m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
237                 
238                 std::vector<WrapperVehicle*>    m_wrapperVehicles;
239
240                 //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to 
241                 //btDiscreteDynamicsWorld::addRigidBody(body,filter,group) 
242                 //so that we can set the body collision filter/group at the time of creation 
243                 //and not afterwards (breaks the collision system for radar/near sensor)
244                 //Ideally we would like to have access to this function from the btDynamicsWorld interface
245                 //class btDynamicsWorld*        m_dynamicsWorld;
246                 class   btSoftRigidDynamicsWorld*       m_dynamicsWorld;
247                 
248                 class btConstraintSolver*       m_solver;
249
250                 class btOverlappingPairCache* m_ownPairCache;
251
252                 class CcdOverlapFilterCallBack* m_filterCallback;
253
254                 class btDispatcher* m_ownDispatcher;
255
256                 bool    m_scalingPropagated;
257
258                 
259
260 };
261
262 #endif //CCDPHYSICSENVIRONMENT