Blender V2.61 - r43446

CcdPhysicsEnvironment.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00021 #ifndef CCDPHYSICSENVIRONMENT
00022 #define CCDPHYSICSENVIRONMENT
00023 
00024 #include "PHY_IPhysicsEnvironment.h"
00025 #include <vector>
00026 #include <set>
00027 class CcdPhysicsController;
00028 class CcdGraphicController;
00029 #include "LinearMath/btVector3.h"
00030 #include "LinearMath/btTransform.h"
00031 
00032 
00033 
00034 
00035 class btTypedConstraint;
00036 class btSimulationIslandManager;
00037 class btCollisionDispatcher;
00038 class btDispatcher;
00039 //#include "btBroadphaseInterface.h"
00040 
00041 //switch on/off new vehicle support
00042 #define NEW_BULLET_VEHICLE_SUPPORT 1
00043 
00044 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
00045 
00046 class WrapperVehicle;
00047 class btPersistentManifold;
00048 class btBroadphaseInterface;
00049 struct btDbvtBroadphase;
00050 class btOverlappingPairCache;
00051 class btIDebugDraw;
00052 class PHY_IVehicle;
00053 class CcdOverlapFilterCallBack;
00054 
00060 class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
00061 {
00062     friend class CcdOverlapFilterCallBack;
00063     btVector3 m_gravity;
00064 
00065 protected:
00066     btIDebugDraw*   m_debugDrawer;
00067     
00068     class btDefaultCollisionConfiguration* m_collisionConfiguration;
00069     class btBroadphaseInterface*        m_broadphase;   // broadphase for dynamic world
00070     // for culling only
00071     btOverlappingPairCache*             m_cullingCache;
00072     struct btDbvtBroadphase*            m_cullingTree;  // broadphase for culling
00073 
00074     //solver iterations
00075     int m_numIterations;
00076     
00077     //timestep subdivisions
00078     int m_numTimeSubSteps;
00079 
00080 
00081     int m_ccdMode;
00082     int m_solverType;
00083     int m_profileTimings;
00084     bool m_enableSatCollisionDetection;
00085 
00086     btContactSolverInfo m_solverInfo;
00087     
00088     void    processFhSprings(double curTime,float timeStep);
00089 
00090     public:
00091         CcdPhysicsEnvironment(bool useDbvtCulling, btDispatcher* dispatcher=0, btOverlappingPairCache* pairCache=0);
00092 
00093         virtual     ~CcdPhysicsEnvironment();
00094 
00096         //PHY_IPhysicsEnvironment interface
00098 
00100 
00101         virtual void setDebugDrawer(btIDebugDraw* debugDrawer);
00102 
00103         virtual void        setNumIterations(int numIter);
00104         virtual void        setNumTimeSubSteps(int numTimeSubSteps)
00105         {
00106             m_numTimeSubSteps = numTimeSubSteps;
00107         }
00108         virtual void        setDeactivationTime(float dTime);
00109         virtual void        setDeactivationLinearTreshold(float linTresh) ;
00110         virtual void        setDeactivationAngularTreshold(float angTresh) ;
00111         virtual void        setContactBreakingTreshold(float contactBreakingTreshold) ;
00112         virtual void        setCcdMode(int ccdMode);
00113         virtual void        setSolverType(int solverType);
00114         virtual void        setSolverSorConstant(float sor);
00115         virtual void        setSolverTau(float tau);
00116         virtual void        setSolverDamping(float damping);
00117         virtual void        setLinearAirDamping(float damping);
00118         virtual void        setUseEpa(bool epa) ;
00119 
00120         virtual void        beginFrame();
00121         virtual void        endFrame() {};
00123         virtual bool        proceedDeltaTime(double curTime,float timeStep,float interval);
00124         
00125         virtual void        debugDrawWorld();
00126 //      virtual bool        proceedDeltaTimeOneStep(float timeStep);
00127 
00128         virtual void        setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep)
00129         {
00130             //based on DEFAULT_PHYSICS_TIC_RATE of 60 hertz
00131             setNumTimeSubSteps((int)(fixedTimeStep / 60.f));
00132         }
00133         //returns 0.f if no fixed timestep is used
00134 
00135         virtual float       getFixedTimeStep(){ return 0.f;};
00136 
00137         virtual void        setDebugMode(int debugMode);
00138 
00139         virtual void        setGravity(float x,float y,float z);
00140         virtual void        getGravity(PHY__Vector3& grav);
00141 
00142 
00143         virtual int         createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
00144             float pivotX,float pivotY,float pivotZ,
00145             float axisX,float axisY,float axisZ,
00146             float axis1X=0,float axis1Y=0,float axis1Z=0,
00147             float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0
00148             );
00149 
00150 
00151         //Following the COLLADA physics specification for constraints
00152         virtual int         createUniversalD6Constraint(
00153         class PHY_IPhysicsController* ctrlRef,class PHY_IPhysicsController* ctrlOther,
00154             btTransform& localAttachmentFrameRef,
00155             btTransform& localAttachmentOther,
00156             const btVector3& linearMinLimits,
00157             const btVector3& linearMaxLimits,
00158             const btVector3& angularMinLimits,
00159             const btVector3& angularMaxLimits,int flags
00160             );
00161 
00162         
00163         virtual void    setConstraintParam(int constraintId,int param,float value,float value1);
00164         
00165         virtual float   getConstraintParam(int constraintId,int param);
00166 
00167         virtual void        removeConstraint(int    constraintid);
00168 
00169         virtual float       getAppliedImpulse(int   constraintid);
00170 
00171 
00172         virtual void    CallbackTriggers();
00173 
00174 
00175 #ifdef NEW_BULLET_VEHICLE_SUPPORT
00176         //complex constraint for vehicles
00177         virtual PHY_IVehicle*   getVehicleConstraint(int constraintId);
00178 #else
00179         virtual class PHY_IVehicle* getVehicleConstraint(int constraintId)
00180         {
00181             return 0;
00182         }
00183 #endif //NEW_BULLET_VEHICLE_SUPPORT
00184 
00185         btTypedConstraint*  getConstraintById(int constraintId);
00186 
00187         virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
00188         virtual bool cullingTest(PHY_CullingCallback callback, void* userData, PHY__Vector4* planes, int nplanes, int occlusionRes);
00189 
00190 
00191         //Methods for gamelogic collision/physics callbacks
00192         virtual void addSensor(PHY_IPhysicsController* ctrl);
00193         virtual void removeSensor(PHY_IPhysicsController* ctrl);
00194         virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
00195         virtual bool requestCollisionCallback(PHY_IPhysicsController* ctrl);
00196         virtual bool removeCollisionCallback(PHY_IPhysicsController* ctrl);
00197         //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else
00198         virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
00199         virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
00200     
00201 
00202         virtual int getNumContactPoints();
00203 
00204         virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
00205 
00207         //CcdPhysicsEnvironment interface
00209     
00210         void    addCcdPhysicsController(CcdPhysicsController* ctrl);
00211 
00212         bool    removeCcdPhysicsController(CcdPhysicsController* ctrl);
00213 
00214         void    updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask);
00215 
00216         void    disableCcdPhysicsController(CcdPhysicsController* ctrl);
00217 
00218         void    enableCcdPhysicsController(CcdPhysicsController* ctrl);
00219 
00220         void    refreshCcdPhysicsController(CcdPhysicsController* ctrl);
00221 
00222         void    addCcdGraphicController(CcdGraphicController* ctrl);
00223 
00224         void    removeCcdGraphicController(CcdGraphicController* ctrl);
00225 
00226         btBroadphaseInterface*  getBroadphase();
00227         btDbvtBroadphase*   getCullingTree() { return m_cullingTree; }
00228 
00229         btDispatcher*   getDispatcher();
00230         
00231 
00232         bool    IsSatCollisionDetectionEnabled() const
00233         {
00234             return m_enableSatCollisionDetection;
00235         }
00236 
00237         void    EnableSatCollisionDetection(bool enableSat)
00238         {
00239             m_enableSatCollisionDetection = enableSat;
00240         }
00241 
00242     
00243         const btPersistentManifold* GetManifold(int index) const;
00244 
00245     
00246         void    SyncMotionStates(float timeStep);
00247 
00248         class   btSoftRigidDynamicsWorld*   getDynamicsWorld()
00249         {
00250             return m_dynamicsWorld;
00251         }
00252     
00253         class btConstraintSolver*   GetConstraintSolver();
00254 
00255         void MergeEnvironment(CcdPhysicsEnvironment *other);
00256 
00257     protected:
00258         
00259         
00260 
00261         std::set<CcdPhysicsController*> m_controllers;
00262         std::set<CcdPhysicsController*> m_triggerControllers;
00263 
00264         PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
00265         void*           m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
00266         
00267         std::vector<WrapperVehicle*>    m_wrapperVehicles;
00268 
00269         //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to 
00270         //btDiscreteDynamicsWorld::addRigidBody(body,filter,group) 
00271         //so that we can set the body collision filter/group at the time of creation 
00272         //and not afterwards (breaks the collision system for radar/near sensor)
00273         //Ideally we would like to have access to this function from the btDynamicsWorld interface
00274         //class btDynamicsWorld*    m_dynamicsWorld;
00275         class   btSoftRigidDynamicsWorld*   m_dynamicsWorld;
00276         
00277         class btConstraintSolver*   m_solver;
00278 
00279         class btOverlappingPairCache* m_ownPairCache;
00280 
00281         class CcdOverlapFilterCallBack* m_filterCallback;
00282 
00283         class btDispatcher* m_ownDispatcher;
00284 
00285         bool    m_scalingPropagated;
00286 
00287         virtual void    exportFile(const char* filename);
00288 
00289         
00290 #ifdef WITH_CXX_GUARDEDALLOC
00291 public:
00292     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:CcdPhysicsEnvironment"); }
00293     void operator delete( void *mem ) { MEM_freeN(mem); }
00294 #endif
00295 };
00296 
00297 #endif //CCDPHYSICSENVIRONMENT