merge with 2.5 at r18679
[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         void    processFhSprings(double curTime,float timeStep);
78
79         public:
80                 CcdPhysicsEnvironment(btDispatcher* dispatcher=0, btOverlappingPairCache* pairCache=0);
81
82                 virtual         ~CcdPhysicsEnvironment();
83
84                 /////////////////////////////////////
85                 //PHY_IPhysicsEnvironment interface
86                 /////////////////////////////////////
87
88                 /// Perform an integration step of duration 'timeStep'.
89
90                 virtual void setDebugDrawer(btIDebugDraw* debugDrawer);
91
92                 virtual void            setNumIterations(int numIter);
93                 virtual void            setNumTimeSubSteps(int numTimeSubSteps)
94                 {
95                         m_numTimeSubSteps = numTimeSubSteps;
96                 }
97                 virtual void            setDeactivationTime(float dTime);
98                 virtual void            setDeactivationLinearTreshold(float linTresh) ;
99                 virtual void            setDeactivationAngularTreshold(float angTresh) ;
100                 virtual void            setContactBreakingTreshold(float contactBreakingTreshold) ;
101                 virtual void            setCcdMode(int ccdMode);
102                 virtual void            setSolverType(int solverType);
103                 virtual void            setSolverSorConstant(float sor);
104                 virtual void            setSolverTau(float tau);
105                 virtual void            setSolverDamping(float damping);
106                 virtual void            setLinearAirDamping(float damping);
107                 virtual void            setUseEpa(bool epa) ;
108
109                 virtual void            beginFrame();
110                 virtual void            endFrame() {};
111                 /// Perform an integration step of duration 'timeStep'.
112                 virtual bool            proceedDeltaTime(double curTime,float timeStep);
113 //              virtual bool            proceedDeltaTimeOneStep(float timeStep);
114
115                 virtual void            setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep){};
116                 //returns 0.f if no fixed timestep is used
117
118                 virtual float           getFixedTimeStep(){ return 0.f;};
119
120                 virtual void            setDebugMode(int debugMode);
121
122                 virtual void            setGravity(float x,float y,float z);
123                 virtual void            getGravity(PHY__Vector3& grav);
124
125
126                 virtual int                     createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
127                         float pivotX,float pivotY,float pivotZ,
128                         float axisX,float axisY,float axisZ,
129                         float axis1X=0,float axis1Y=0,float axis1Z=0,
130                         float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0
131                         );
132
133
134                 //Following the COLLADA physics specification for constraints
135                 virtual int                     createUniversalD6Constraint(
136                 class PHY_IPhysicsController* ctrlRef,class PHY_IPhysicsController* ctrlOther,
137                         btTransform& localAttachmentFrameRef,
138                         btTransform& localAttachmentOther,
139                         const btVector3& linearMinLimits,
140                         const btVector3& linearMaxLimits,
141                         const btVector3& angularMinLimits,
142                         const btVector3& angularMaxLimits,int flags
143                         );
144
145                 virtual void    setConstraintParam(int constraintId,int param,float value,float value1);
146
147             virtual void                removeConstraint(int    constraintid);
148
149                 virtual float           getAppliedImpulse(int   constraintid);
150
151
152                 virtual void    CallbackTriggers();
153
154
155 #ifdef NEW_BULLET_VEHICLE_SUPPORT
156                 //complex constraint for vehicles
157                 virtual PHY_IVehicle*   getVehicleConstraint(int constraintId);
158 #else
159                 virtual class PHY_IVehicle*     getVehicleConstraint(int constraintId)
160                 {
161                         return 0;
162                 }
163 #endif //NEW_BULLET_VEHICLE_SUPPORT
164
165                 btTypedConstraint*      getConstraintById(int constraintId);
166
167                 virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
168
169
170                 //Methods for gamelogic collision/physics callbacks
171                 virtual void addSensor(PHY_IPhysicsController* ctrl);
172                 virtual void removeSensor(PHY_IPhysicsController* ctrl);
173                 virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
174                 virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl);
175                 virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl);
176                 //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else
177                 virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
178                 virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
179         
180
181                 virtual int     getNumContactPoints();
182
183                 virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
184
185                 //////////////////////
186                 //CcdPhysicsEnvironment interface
187                 ////////////////////////
188         
189                 void    addCcdPhysicsController(CcdPhysicsController* ctrl);
190
191                 void    removeCcdPhysicsController(CcdPhysicsController* ctrl);
192
193                 void    updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask);
194
195                 void    disableCcdPhysicsController(CcdPhysicsController* ctrl);
196
197                 void    enableCcdPhysicsController(CcdPhysicsController* ctrl);
198
199                 void    refreshCcdPhysicsController(CcdPhysicsController* ctrl);
200
201                 btBroadphaseInterface*  getBroadphase();
202
203                 btDispatcher*   getDispatcher();
204                 
205
206                 bool    IsSatCollisionDetectionEnabled() const
207                 {
208                         return m_enableSatCollisionDetection;
209                 }
210
211                 void    EnableSatCollisionDetection(bool enableSat)
212                 {
213                         m_enableSatCollisionDetection = enableSat;
214                 }
215
216         
217                 const btPersistentManifold*     GetManifold(int index) const;
218
219         
220                 void    SyncMotionStates(float timeStep);
221
222                 class   btSoftRigidDynamicsWorld*       getDynamicsWorld()
223                 {
224                         return m_dynamicsWorld;
225                 }
226         
227                 class btConstraintSolver*       GetConstraintSolver();
228
229         protected:
230                 
231                 
232
233                 
234                 std::set<CcdPhysicsController*> m_controllers;
235                 
236                 std::set<CcdPhysicsController*> m_triggerControllers;
237
238                 PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
239                 void*                   m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
240                 
241                 std::vector<WrapperVehicle*>    m_wrapperVehicles;
242
243                 //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to 
244                 //btDiscreteDynamicsWorld::addRigidBody(body,filter,group) 
245                 //so that we can set the body collision filter/group at the time of creation 
246                 //and not afterwards (breaks the collision system for radar/near sensor)
247                 //Ideally we would like to have access to this function from the btDynamicsWorld interface
248                 //class btDynamicsWorld*        m_dynamicsWorld;
249                 class   btSoftRigidDynamicsWorld*       m_dynamicsWorld;
250                 
251                 class btConstraintSolver*       m_solver;
252
253                 class btOverlappingPairCache* m_ownPairCache;
254
255                 class CcdOverlapFilterCallBack* m_filterCallback;
256
257                 class btDispatcher* m_ownDispatcher;
258
259                 bool    m_scalingPropagated;
260
261                 
262
263 };
264
265 #endif //CCDPHYSICSENVIRONMENT