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