BGE Scenegraph and View frustrum culling improvement.
[blender-staging.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);
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                 virtual void    setConstraintParam(int constraintId,int param,float value,float value1);
153
154             virtual void                removeConstraint(int    constraintid);
155
156                 virtual float           getAppliedImpulse(int   constraintid);
157
158
159                 virtual void    CallbackTriggers();
160
161
162 #ifdef NEW_BULLET_VEHICLE_SUPPORT
163                 //complex constraint for vehicles
164                 virtual PHY_IVehicle*   getVehicleConstraint(int constraintId);
165 #else
166                 virtual class PHY_IVehicle*     getVehicleConstraint(int constraintId)
167                 {
168                         return 0;
169                 }
170 #endif //NEW_BULLET_VEHICLE_SUPPORT
171
172                 btTypedConstraint*      getConstraintById(int constraintId);
173
174                 virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
175                 virtual bool cullingTest(PHY_CullingCallback callback, void* userData, PHY__Vector4* planes, int nplanes);
176
177
178                 //Methods for gamelogic collision/physics callbacks
179                 virtual void addSensor(PHY_IPhysicsController* ctrl);
180                 virtual void removeSensor(PHY_IPhysicsController* ctrl);
181                 virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
182                 virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl);
183                 virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl);
184                 //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else
185                 virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
186                 virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
187         
188
189                 virtual int     getNumContactPoints();
190
191                 virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
192
193                 //////////////////////
194                 //CcdPhysicsEnvironment interface
195                 ////////////////////////
196         
197                 void    addCcdPhysicsController(CcdPhysicsController* ctrl);
198
199                 void    removeCcdPhysicsController(CcdPhysicsController* ctrl);
200
201                 void    updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask);
202
203                 void    disableCcdPhysicsController(CcdPhysicsController* ctrl);
204
205                 void    enableCcdPhysicsController(CcdPhysicsController* ctrl);
206
207                 void    refreshCcdPhysicsController(CcdPhysicsController* ctrl);
208
209                 void    addCcdGraphicController(CcdGraphicController* ctrl);
210
211                 void    removeCcdGraphicController(CcdGraphicController* ctrl);
212
213                 btBroadphaseInterface*  getBroadphase();
214                 btDbvtBroadphase*       getCullingTree() { return m_cullingTree; }
215
216                 btDispatcher*   getDispatcher();
217                 
218
219                 bool    IsSatCollisionDetectionEnabled() const
220                 {
221                         return m_enableSatCollisionDetection;
222                 }
223
224                 void    EnableSatCollisionDetection(bool enableSat)
225                 {
226                         m_enableSatCollisionDetection = enableSat;
227                 }
228
229         
230                 const btPersistentManifold*     GetManifold(int index) const;
231
232         
233                 void    SyncMotionStates(float timeStep);
234
235                 class   btSoftRigidDynamicsWorld*       getDynamicsWorld()
236                 {
237                         return m_dynamicsWorld;
238                 }
239         
240                 class btConstraintSolver*       GetConstraintSolver();
241
242         protected:
243                 
244                 
245
246                 
247                 std::set<CcdPhysicsController*> m_controllers;
248                 
249                 std::set<CcdPhysicsController*> m_triggerControllers;
250
251                 PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
252                 void*                   m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
253                 
254                 std::vector<WrapperVehicle*>    m_wrapperVehicles;
255
256                 //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to 
257                 //btDiscreteDynamicsWorld::addRigidBody(body,filter,group) 
258                 //so that we can set the body collision filter/group at the time of creation 
259                 //and not afterwards (breaks the collision system for radar/near sensor)
260                 //Ideally we would like to have access to this function from the btDynamicsWorld interface
261                 //class btDynamicsWorld*        m_dynamicsWorld;
262                 class   btSoftRigidDynamicsWorld*       m_dynamicsWorld;
263                 
264                 class btConstraintSolver*       m_solver;
265
266                 class btOverlappingPairCache* m_ownPairCache;
267
268                 class CcdOverlapFilterCallBack* m_filterCallback;
269
270                 class btDispatcher* m_ownDispatcher;
271
272                 bool    m_scalingPropagated;
273
274                 
275
276 };
277
278 #endif //CCDPHYSICSENVIRONMENT