- added support for generic 6DOF constraint
[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
46 /// CcdPhysicsEnvironment is an experimental mainloop for physics simulation using optional continuous collision detection.
47 /// Physics Environment takes care of stepping the simulation and is a container for physics entities.
48 /// It stores rigidbodies,constraints, materials etc.
49 /// A derived class may be able to 'construct' entities by loading and/or converting
50 class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
51 {
52         btVector3 m_gravity;
53         
54         
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
122
123                 //Following the COLLADA physics specification for constraints
124                 virtual int                     createUniversalD6Constraint(
125                 class PHY_IPhysicsController* ctrlRef,class PHY_IPhysicsController* ctrlOther,
126                         btTransform& localAttachmentFrameRef,
127                         btTransform& localAttachmentOther,
128                         const btVector3& linearMinLimits,
129                         const btVector3& linearMaxLimits,
130                         const btVector3& angularMinLimits,
131                         const btVector3& angularMaxLimits
132                         );
133
134                 virtual void    setConstraintParam(int constraintId,int param,float value,float value1);
135
136             virtual void                removeConstraint(int    constraintid);
137
138                 virtual float           getAppliedImpulse(int   constraintid);
139
140
141                 virtual void    CallbackTriggers();
142
143
144 #ifdef NEW_BULLET_VEHICLE_SUPPORT
145                 //complex constraint for vehicles
146                 virtual PHY_IVehicle*   getVehicleConstraint(int constraintId);
147 #else
148                 virtual class PHY_IVehicle*     getVehicleConstraint(int constraintId)
149                 {
150                         return 0;
151                 }
152 #endif //NEW_BULLET_VEHICLE_SUPPORT
153
154                 btTypedConstraint*      getConstraintById(int constraintId);
155
156                 virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
157                                                                                 float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
158
159
160                 //Methods for gamelogic collision/physics callbacks
161                 virtual void addSensor(PHY_IPhysicsController* ctrl);
162                 virtual void removeSensor(PHY_IPhysicsController* ctrl);
163                 virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
164                 virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl);
165                 virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl);
166
167                 virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
168                 virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
169         
170
171                 virtual int     getNumContactPoints();
172
173                 virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
174
175                 //////////////////////
176                 //CcdPhysicsEnvironment interface
177                 ////////////////////////
178         
179                 void    addCcdPhysicsController(CcdPhysicsController* ctrl);
180
181                 void    removeCcdPhysicsController(CcdPhysicsController* ctrl);
182
183                 btBroadphaseInterface*  getBroadphase();
184
185                 
186                 
187                 
188
189                 bool    IsSatCollisionDetectionEnabled() const
190                 {
191                         return m_enableSatCollisionDetection;
192                 }
193
194                 void    EnableSatCollisionDetection(bool enableSat)
195                 {
196                         m_enableSatCollisionDetection = enableSat;
197                 }
198
199         
200                 int     GetNumControllers();
201
202                 CcdPhysicsController* GetPhysicsController( int index);
203
204                 
205
206                 const btPersistentManifold*     GetManifold(int index) const;
207
208         
209                 void    SyncMotionStates(float timeStep);
210
211                 
212         
213                 class btConstraintSolver*       GetConstraintSolver();
214
215         protected:
216                 
217                 
218
219                 
220                 std::vector<CcdPhysicsController*> m_controllers;
221                 
222                 std::vector<CcdPhysicsController*> m_triggerControllers;
223
224                 PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
225                 void*                   m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
226                 
227                 std::vector<WrapperVehicle*>    m_wrapperVehicles;
228
229                 class   btDynamicsWorld*        m_dynamicsWorld;
230                 
231                 class btConstraintSolver*       m_solver;
232
233                 bool    m_scalingPropagated;
234
235                 
236
237 };
238
239 #endif //CCDPHYSICSENVIRONMENT