00001 #ifndef _MercuryODE_H
00002 #define _MercuryODE_H
00003
00004 #include <ode/ode.h>
00005 #include "MercuryINI.h"
00006 #include "MercuryObject.h"
00007
00008 class MercuryODEWorld;
00009
00011 class MercuryODEObject : public MercuryObject
00012 {
00013 public:
00014 MercuryODEObject();
00015 virtual ~MercuryODEObject();
00016
00018 virtual bool Collide( MercuryODEObject * pHit, dContact & pContact, MercuryODEWorld * pWorld ) = 0;
00019
00021 dMass m_oAccumulatedMass;
00023 dBodyID m_oBody;
00025 dSpaceID m_oSpace;
00026
00027 PSElement GetParameter( const MString & sName ) { return m_mParameters[sName]; }
00028 void SetParameter( const MString & sName, const PSElement & pSet ) { m_mParameters[sName] = pSet; }
00029
00031 virtual void PreCycleUpdate( const float dTime ) { }
00032 CLASS_RTTI( MercuryODEObject, MercuryObject );
00033
00034 virtual void RemoveODE();
00035
00036 MercuryODEWorld * m_pWorld;
00037 protected:
00038 bool m_wasActive;
00039 private:
00040 std::map< MString, PSElement > m_mParameters;
00041 };
00042
00044 class MercuryODEObjectLoadable : public MercuryODEObject
00045 {
00046 public:
00047 MercuryODEObjectLoadable()
00048 :MercuryODEObject()
00049 { }
00050 MercuryODEObjectLoadable( const MString & sName )
00051 :MercuryODEObject()
00052 {
00053 SetName(sName);
00054 }
00055
00056 virtual ~MercuryODEObjectLoadable();
00057 virtual void Update( const float dTime );
00058 virtual void RemoveODE();
00059
00060 virtual bool Collide( MercuryODEObject * pHit, dContact & pContact, MercuryODEWorld * pCollide ) { return true; }
00061
00063 virtual bool LoadFromINI( MercuryINI & pINI, const MString & sShapeName, const dWorldID &oWorld );
00064
00066 bool m_bBodied;
00067
00068 MercuryPoint m_pPrePosition;
00069 MercuryPoint m_pPreRotation;
00070
00071 MVector< dGeomID > m_vAllGeoms;
00072 MVector< dGeomID > m_vAllTransforms;
00073
00074 dSpaceID m_oWorldSpace;
00075 CLASS_RTTI( MercuryODEObjectLoadable, MercuryODEObject );
00076
00077 MVector< MercuryObject * > m_vMercuryShapes;
00078
00079 protected:
00080 dWorldID m_oParentWorld;
00081 private:
00082
00084 struct InternalTrimesh
00085 {
00086
00087
00088 ~InternalTrimesh()
00089 {
00090 dGeomTriMeshDataDestroy( m_oTriData );
00091 delete [] m_aVerts;
00092 delete [] m_aIndices;
00093 }
00094
00096 dTriMeshDataID m_oTriData;
00097
00099 dVector3 * m_aVerts;
00100 int m_iVertexCount;
00101
00103 int * m_aIndices;
00104 int m_iIndexCount;
00105 };
00106
00108 MVector< InternalTrimesh * > m_vTrimeshes;
00109 };
00110
00112
00128 class MercuryODEWorld : public MercuryObject
00129 {
00130 public:
00131 MercuryODEWorld();
00132 virtual ~MercuryODEWorld();
00133
00134 virtual void Init();
00135
00137 bool LoadFromINI( MercuryINI & pINI, const MString & sWorldName );
00138
00140 void AddToWorld( MercuryODEObject * toAdd );
00141
00143 MercuryODEObject * GenAndAddToWorld( MercuryINI & pINI, const MString & sItemName, const MString & sItemType, const MString & sClass, bool bBodied, const MercuryPoint & pInitialPosition );
00144
00146 virtual bool RemoveObject( MercuryObject * object, bool bAllowDelete = true );
00147
00149 MercuryODEObject * GetODEObjectByName( const MString & name ) { return m_mObjects[name]; }
00150
00152 dSpaceID GetWorldSpaceID() { return m_oSpace; }
00153
00155 dWorldID GetWorldID() { return m_oWorld; }
00156
00157 virtual void Update( const float fDeltaTime );
00158
00160 void SetGravity( const float fGravity );
00161
00162 CLASS_RTTI( MercuryODEWorld, MercuryObject );
00163
00164 MHash<MercuryODEObject *> & GetAllObjectMap() { return m_mObjects; }
00165
00167 dJointGroupID m_oJointGroup;
00168 protected:
00170 MHash< MercuryODEObject * > m_mObjects;
00171
00173 dWorldID m_oWorld;
00174
00176 dSpaceID m_oSpace;
00177
00179 dGeomID m_oGround;
00180
00182 friend void nearCallback (void *data, dGeomID o1, dGeomID o2);
00183 };
00184
00185
00186
00187
00188 typedef MercuryODEObjectLoadable* (*NewODEObjectFunction)(const MString& sClassName);
00189 void RegisterODEType( const MString& sClassName, NewODEObjectFunction pfn );
00190
00191 #include "MercuryObjectFactory.h"
00192
00193 #define REGISTER_ODE_OBJECT_CLASS( className ) \
00194 static MercuryODEObjectLoadable* CreateODE##className( const MString &sName ) \
00195 { MercuryODEObjectLoadable *pRet = new className( sName ); return pRet; } \
00196 struct RegisterODE##className { \
00197 RegisterODE##className() { RegisterODEType( #className,CreateODE##className); } \
00198 }; \
00199 static RegisterODE##className register_ODE##className; \
00200 REGISTER_OBJECT_TYPE( className );
00201
00202 extern std::map< MString, NewODEObjectFunction > *ToMakeODEs;
00203
00204
00205
00206
00207 #endif
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235