00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef PHY_WORLD_H
00028 #define PHY_WORLD_H
00029 
00030 
00031 #ifndef ENABLE_PHYSICS_BASIC_WRAPPING
00032     
00033 #endif
00034 #ifndef ENABLE_PHYSICS_EXTRA_WRAPPING
00035     
00036 #endif
00037 
00038 #include <PVLE/Export.h>
00039 #include <osg/Vec3>
00040 #include <osg/ref_ptr>
00041 #include <osg/Referenced>
00042 #include <PVLE/Util/Singleton.h>
00043 #include <PVLE/Util/TracedException.h>
00044 
00045 #include <ode/ode.h>
00046 #include <PVLE/Physics/Converters.h>
00047 #include <PVLE/Physics/Space.h>
00048 #include <PVLE/Physics/Joint.h>
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00068 namespace Physics {
00069 
00070 class AbstractGeom;
00071 class World;
00072 
00073 
00075 class NearCallback {
00076 public:
00078     virtual void operator()(World * pWorld, AbstractGeom * pG1, AbstractGeom * pG2);
00079 
00081     static void toPVLENearCallback(void * data, dGeomID o1, dGeomID o2);
00082 };
00083 
00084 
00085 
00086 
00087 inline void dWorldSetGravity(dWorldID id, const osg::Vec3 & force) { dWorldSetGravity(id, force.x(), force.y(), force.z()); }
00088 inline osg::Vec3 dWorldGetGravityV(dWorldID id) { dVector3 v; dWorldGetGravity(id, v); return toGraphVec3(v); }
00089 
00090 
00091 
00092 
00093 
00098 class PVLE_EXPORT World : public osg::Referenced {
00099 public:
00102     World();
00105     World(const osg::Vec3 & worldBox);
00107     World(Space * space);
00108 
00109 #ifdef ENABLE_PHYSICS_BASIC_WRAPPING
00110     void setGravity(const osg::Vec3 & force) { dWorldSetGravity(id, force.x(), force.y(), force.z()); }
00111     osg::Vec3 getGravity() const { dVector3 v; dWorldGetGravity(id, v); return toGraphVec3(v); }
00112 #endif
00113 
00114     void setUseQuickStep(bool flag) { bUseQuick = flag; }       
00115     bool getUseQuickStep() const { return bUseQuick; }
00116 
00117 #ifdef ENABLE_PHYSICS_EXTRA_WRAPPING
00119     void setQuickStepNumIterations(int n) { dWorldSetQuickStepNumIterations(id, n); }
00120     int getQuickStepNumIterations() const { return dWorldGetQuickStepNumIterations(id); }
00121 
00122     void setERP(dReal erp) { dWorldSetERP (id, erp); }
00123     dReal getERP() { return dWorldGetERP (id); }
00124 
00125     void setCFM(dReal cfm) { dWorldSetCFM (id, cfm); }
00126     dReal getCFM() { return dWorldGetCFM (id); }
00127 #endif
00128 
00130     void step(dReal stepSize);
00131 
00132     Space * getGlobalSpace() { return pSpace.get(); }
00133     const Space * getGlobalSpace() const { return pSpace.get(); }
00134 
00135     void setNearCallback(NearCallback * pNcb) { this->pNcb = pNcb; }
00136     NearCallback * getNearCallback() { return pNcb; }
00137     const NearCallback * getNearCallback() const { return pNcb; }
00138 
00139     JointGroup * getContactGroup() { return pContactGroup.get(); };
00140     const JointGroup * getContactGroup() const { return pContactGroup.get(); };
00141 
00142 #ifdef ENABLE_PHYSICS_EXTRA_WRAPPING
00145     void setContactSurfaceLayer(dReal depth) { dWorldSetContactSurfaceLayer(id, depth); }
00146     dReal getContactSurfaceLayer() { return dWorldGetContactSurfaceLayer(id); }
00147 
00148     void setContactMaxCorrectingVel(dReal vel) { dWorldSetContactMaxCorrectingVel(id, vel); }
00149     dReal getContactMaxCorrectingVel() { return dWorldGetContactMaxCorrectingVel(id); }
00151 
00155 
00157     void setAutoDisable(bool enable) { dWorldSetAutoDisableFlag(id, enable ? 1 : 0); }
00159     bool getAutoDisable() const { return dWorldGetAutoDisableFlag(id)!=0; }
00160 
00163     void setAutoEnableDepthSF1(bool enable) { dWorldSetAutoEnableDepthSF1(id, enable ? 1 : 0); }
00165     bool getAutoEnableDepthSF1() const { return dWorldGetAutoEnableDepthSF1(id)!=0; }
00166 
00168     void setAutoDisableLinearThreshold(dReal value) { dWorldSetAutoDisableLinearThreshold(id, value); }
00170     dReal getAutoDisableLinearThreshold() const { return dWorldGetAutoDisableLinearThreshold(id); }
00171 
00173     void setAutoDisableAngularThreshold(dReal value) { dWorldSetAutoDisableAngularThreshold(id, value); }
00175     dReal getAutoDisableAngularThreshold() const { return dWorldGetAutoDisableAngularThreshold(id); }
00176 
00178     
00180     
00181 
00183     
00185     
00186 
00188     void setAutoDisableSteps(dReal value) { dWorldSetAutoDisableSteps(id, value); }
00190     dReal getAutoDisableSteps() const { return dWorldGetAutoDisableSteps(id); }
00191 
00193     void setAutoDisableTime(dReal value) { dWorldSetAutoDisableTime(id, value); }
00195     dReal getAutoDisableTime() const { return dWorldGetAutoDisableTime(id); }
00196 
00199     void setAutoDisableAverageSamplesCount(unsigned int value) { dWorldSetAutoDisableAverageSamplesCount(id, value); }
00201     dReal getAutoDisableAverageSamplesCount() const { return dWorldGetAutoDisableAverageSamplesCount(id); }
00202 
00204 
00207 
00209     void setMaxAngularVel(dReal maxAngularVel) { dWorldSetMaxAngularSpeed(id, maxAngularVel); }
00211     dReal getMaxAngularVel() const { return dWorldGetMaxAngularSpeed(id); }
00212 
00213     
00214     
00215     
00216     
00217     
00218     
00219     
00220     
00221     
00222 
00224 #endif
00225 
00226     operator dWorldID() { return id; }
00227     operator const dWorldID() const { return id; }
00228 
00229 protected:
00230     friend class Body;
00231     friend class Joint;             
00232     friend class JointGroup;        
00233     friend class NearCallback;      
00234 
00235     mutable dWorldID id;
00236     osg::ref_ptr<Space> pSpace;     
00237     osg::ref_ptr<JointGroup> pContactGroup;
00238 
00239     bool bUseQuick;
00240 
00241     void init();
00242 
00244     class ODEHolder : public Util::Singleton<ODEHolder> {
00245     public:
00246         ODEHolder() {
00247             int res = dInitODE2(0);
00248             if (res==0) THROW_TRACED_EXCEPTION("Unable to initialize ODE physics engine");
00249         }
00250         virtual ~ODEHolder() { dCloseODE(); }
00251     };
00252 
00253     NearCallback * pNcb;
00254 
00256     void resetSpaceCollideFlag(Space * pCurSpace);
00258     void spaceCollide(Space * pCurSpace);
00259     void spaceCollide2(AbstractGeom * pAGeom1, AbstractGeom * pAGeom2);
00260 
00263     virtual ~World();       
00264 };
00265 
00266 
00267 }   
00268 
00269 #endif  // PHY_WORLD_H