2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
16 /** \file CcdPhysicsEnvironment.h
18 * See also \ref bulletdoc
21 #ifndef __CCDPHYSICSENVIRONMENT_H__
22 #define __CCDPHYSICSENVIRONMENT_H__
24 #include "PHY_IPhysicsEnvironment.h"
27 class CcdPhysicsController;
28 class CcdGraphicController;
29 #include "LinearMath/btVector3.h"
30 #include "LinearMath/btTransform.h"
35 class btTypedConstraint;
36 class btSimulationIslandManager;
37 class btCollisionDispatcher;
39 //#include "btBroadphaseInterface.h"
41 //switch on/off new vehicle support
42 #define NEW_BULLET_VEHICLE_SUPPORT 1
44 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
47 class btPersistentManifold;
48 class btBroadphaseInterface;
49 struct btDbvtBroadphase;
50 class btOverlappingPairCache;
53 class CcdOverlapFilterCallBack;
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
60 class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
62 friend class CcdOverlapFilterCallBack;
66 btIDebugDraw* m_debugDrawer;
68 class btDefaultCollisionConfiguration* m_collisionConfiguration;
69 class btBroadphaseInterface* m_broadphase; // broadphase for dynamic world
71 btOverlappingPairCache* m_cullingCache;
72 struct btDbvtBroadphase* m_cullingTree; // broadphase for culling
77 //timestep subdivisions
78 int m_numTimeSubSteps;
84 bool m_enableSatCollisionDetection;
86 btContactSolverInfo m_solverInfo;
88 void processFhSprings(double curTime,float timeStep);
91 CcdPhysicsEnvironment(bool useDbvtCulling, btDispatcher* dispatcher=0, btOverlappingPairCache* pairCache=0);
93 virtual ~CcdPhysicsEnvironment();
95 /////////////////////////////////////
96 //PHY_IPhysicsEnvironment interface
97 /////////////////////////////////////
99 /// Perform an integration step of duration 'timeStep'.
101 virtual void setDebugDrawer(btIDebugDraw* debugDrawer);
103 virtual void setNumIterations(int numIter);
104 virtual void setNumTimeSubSteps(int numTimeSubSteps)
106 m_numTimeSubSteps = numTimeSubSteps;
108 virtual void setDeactivationTime(float dTime);
109 virtual void setDeactivationLinearTreshold(float linTresh);
110 virtual void setDeactivationAngularTreshold(float angTresh);
111 virtual void setContactBreakingTreshold(float contactBreakingTreshold);
112 virtual void setCcdMode(int ccdMode);
113 virtual void setSolverType(int solverType);
114 virtual void setSolverSorConstant(float sor);
115 virtual void setSolverTau(float tau);
116 virtual void setSolverDamping(float damping);
117 virtual void setLinearAirDamping(float damping);
118 virtual void setUseEpa(bool epa);
120 virtual void beginFrame();
121 virtual void endFrame() {}
122 /// Perform an integration step of duration 'timeStep'.
123 virtual bool proceedDeltaTime(double curTime,float timeStep,float interval);
125 virtual void debugDrawWorld();
126 // virtual bool proceedDeltaTimeOneStep(float timeStep);
128 virtual void setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep)
130 //based on DEFAULT_PHYSICS_TIC_RATE of 60 hertz
131 setNumTimeSubSteps((int)(fixedTimeStep / 60.f));
133 //returns 0.f if no fixed timestep is used
135 virtual float getFixedTimeStep() { return 0.f; }
137 virtual void setDebugMode(int debugMode);
139 virtual void setGravity(float x,float y,float z);
140 virtual void getGravity(PHY__Vector3& grav);
143 virtual int createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
144 float pivotX,float pivotY,float pivotZ,
145 float axisX,float axisY,float axisZ,
146 float axis1X=0,float axis1Y=0,float axis1Z=0,
147 float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0
151 //Following the COLLADA physics specification for constraints
152 virtual int createUniversalD6Constraint(
153 class PHY_IPhysicsController* ctrlRef,class PHY_IPhysicsController* ctrlOther,
154 btTransform& localAttachmentFrameRef,
155 btTransform& localAttachmentOther,
156 const btVector3& linearMinLimits,
157 const btVector3& linearMaxLimits,
158 const btVector3& angularMinLimits,
159 const btVector3& angularMaxLimits,int flags
163 virtual void setConstraintParam(int constraintId,int param,float value,float value1);
165 virtual float getConstraintParam(int constraintId,int param);
167 virtual void removeConstraint(int constraintid);
169 virtual float getAppliedImpulse(int constraintid);
172 virtual void CallbackTriggers();
175 #ifdef NEW_BULLET_VEHICLE_SUPPORT
176 //complex constraint for vehicles
177 virtual PHY_IVehicle* getVehicleConstraint(int constraintId);
179 virtual class PHY_IVehicle* getVehicleConstraint(int constraintId)
183 #endif //NEW_BULLET_VEHICLE_SUPPORT
185 btTypedConstraint* getConstraintById(int constraintId);
187 virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
188 virtual bool cullingTest(PHY_CullingCallback callback, void* userData, PHY__Vector4* planes, int nplanes, int occlusionRes);
191 //Methods for gamelogic collision/physics callbacks
192 virtual void addSensor(PHY_IPhysicsController* ctrl);
193 virtual void removeSensor(PHY_IPhysicsController* ctrl);
194 virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
195 virtual bool requestCollisionCallback(PHY_IPhysicsController* ctrl);
196 virtual bool removeCollisionCallback(PHY_IPhysicsController* ctrl);
197 //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else
198 virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
199 virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
202 virtual int getNumContactPoints();
204 virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
206 //////////////////////
207 //CcdPhysicsEnvironment interface
208 ////////////////////////
210 void addCcdPhysicsController(CcdPhysicsController* ctrl);
212 bool removeCcdPhysicsController(CcdPhysicsController* ctrl);
214 void updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask);
216 void disableCcdPhysicsController(CcdPhysicsController* ctrl);
218 void enableCcdPhysicsController(CcdPhysicsController* ctrl);
220 void refreshCcdPhysicsController(CcdPhysicsController* ctrl);
222 void addCcdGraphicController(CcdGraphicController* ctrl);
224 void removeCcdGraphicController(CcdGraphicController* ctrl);
226 btBroadphaseInterface* getBroadphase();
227 btDbvtBroadphase* getCullingTree() { return m_cullingTree; }
229 btDispatcher* getDispatcher();
232 bool IsSatCollisionDetectionEnabled() const
234 return m_enableSatCollisionDetection;
237 void EnableSatCollisionDetection(bool enableSat)
239 m_enableSatCollisionDetection = enableSat;
243 const btPersistentManifold* GetManifold(int index) const;
246 void SyncMotionStates(float timeStep);
248 class btSoftRigidDynamicsWorld* getDynamicsWorld()
250 return m_dynamicsWorld;
253 class btConstraintSolver* GetConstraintSolver();
255 void MergeEnvironment(CcdPhysicsEnvironment *other);
261 std::set<CcdPhysicsController*> m_controllers;
262 std::set<CcdPhysicsController*> m_triggerControllers;
264 PHY_ResponseCallback m_triggerCallbacks[PHY_NUM_RESPONSE];
265 void* m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
267 std::vector<WrapperVehicle*> m_wrapperVehicles;
269 //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to
270 //btDiscreteDynamicsWorld::addRigidBody(body,filter,group)
271 //so that we can set the body collision filter/group at the time of creation
272 //and not afterwards (breaks the collision system for radar/near sensor)
273 //Ideally we would like to have access to this function from the btDynamicsWorld interface
274 //class btDynamicsWorld* m_dynamicsWorld;
275 class btSoftRigidDynamicsWorld* m_dynamicsWorld;
277 class btConstraintSolver* m_solver;
279 class btOverlappingPairCache* m_ownPairCache;
281 class CcdOverlapFilterCallBack* m_filterCallback;
283 class btDispatcher* m_ownDispatcher;
285 bool m_scalingPropagated;
287 virtual void exportFile(const char* filename);
290 #ifdef WITH_CXX_GUARDEDALLOC
292 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:CcdPhysicsEnvironment"); }
293 void operator delete( void *mem ) { MEM_freeN(mem); }
297 #endif //__CCDPHYSICSENVIRONMENT_H__