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