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