接上文介紹了ode的基本函數(shù),接下來應(yīng)該考慮如何綁定Ode和游戲引擎
簡單的說,單單使用ode不是什么難事情,必須網(wǎng)上使用ode的例子一大把
問題的關(guān)鍵是如何較好的結(jié)合ode和引擎,記不至于游戲引擎把ode封裝的太深以至于影響其使用的靈活性.另外一方面是,也不能把過多的額外操作拋給用戶操作。
不過可以先設(shè)計一個基本的框架,以后根據(jù)需要和認(rèn)識的不斷加深再做修改。
于是先寫下物理引擎的基本框架-很簡單的了:
1 ////////////////////////////////////////////////////////////
2 /// 定義物理引擎模板類
3 ////////////////////////////////////////////////////////////
4 template<class World,class Space,class JointGroup,class Geom>
5 class PhysicsEngine
6 {
7 public:
8 typedef void(*ContactNearCallback)(void *data, Geom o1, Geom o2);
9 public:
10 ///////////////////////////////////////////////////////
11 /// 構(gòu)造,析構(gòu)物理引擎類
12 ///////////////////////////////////////////////////////
13 PhysicsEngine();
14 ~PhysicsEngine();
15 public:
16 ///////////////////////////////////////////////////////
17 /// 設(shè)定物理引擎平面(ax+by+cz+d=0)
18 ///////////////////////////////////////////////////////
19 void SetPlane(float a = 0, float b = 0, float c = 1, float d = 0);
20
21 ///////////////////////////////////////////////////////
22 /// 設(shè)定物理世界重力加速度
23 ///////////////////////////////////////////////////////
24 void SetGravity(float x = 0, float y = 0, float z = -9.81);
25
26 ///////////////////////////////////////////////////////
27 /// 設(shè)定物理世界仿真更新值
28 ///////////////////////////////////////////////////////
29 inline void SetStepValue(float step = 0.05f, bool quick = true)
30 {
31 this->step = step; this->quick = quick;
32 }
33
34 ///////////////////////////////////////////////////////
35 /// 加載引擎物理對象碰撞回調(diào)函數(shù)
36 ///////////////////////////////////////////////////////
37 inline void SetCollideCallBack(ContactNearCallback callback)
38 {
39 this->callback = callback;
40 }
41
42 ///////////////////////////////////////////////////////
43 /// 調(diào)用物理引擎模擬
44 ///////////////////////////////////////////////////////
45 void Simulation();
46
47 ///////////////////////////////////////////////////////
48 /// 獲取物理引擎世界,空間和關(guān)節(jié)組對象.
49 ///////////////////////////////////////////////////////
50 inline World GetWorld(){return world;}
51 inline Space GetSpace(){return space;}
52 inline JointGroup GetContactGroup(){return contactgroup;}
53 private:
54 World world;
55 Space space;
56 JointGroup contactgroup;
57 ContactNearCallback callback;
58 float step;
59 bool quick;
60 };
61
62 #ifdef PHYSICS_EXT_ODE
63 #include "OdePhysics.inl"
64 #endif
具體對于ode,我看可以這樣使用之:
1 core::PhysicsEngine<dWorldID,dSpaceID,dJointGroupID,dGeomID> engine;
或者可以這樣
1 core::PhysicsEngine<> engine;
再給出一個簡單的物理對象數(shù)據(jù)結(jié)構(gòu)
1 ////////////////////////////////////////////////////////////
2 /// 定義引擎物理對象數(shù)據(jù)結(jié)構(gòu)(一個對象由N個幾何體構(gòu)成)
3 ////////////////////////////////////////////////////////////
4 /*template<class Object,class Geom,int N>
5 struct PhysicsObject
6 {
7 Object object;
8 //! 有必要設(shè)計一個定長數(shù)組了.
9 Geom geom[N];
10 };*/
一個物理對象由N個簡單幾何體構(gòu)成
這個是簡單到了極點只是不太符合要求
下一步該考慮設(shè)計物理對象類了
對象可以旋轉(zhuǎn),平移,設(shè)置質(zhì)量,加掛幾何體.......
最后再上幾個ode函數(shù)加深一點認(rèn)識吧:
1 /**
2 * @brief Set the local offset rotation matrix of a geom from its body.
3 *
4 * Sets the geom's rotational offset in local coordinates.
5 * After this call, the geom will be at a new position determined from the
6 * body's position and the offset.
7 * The geom must be attached to a body.
8 * If the geom did not have an offset, it is automatically created.
9 *
10 * @param geom the geom to set.
11 * @param R the new rotation matrix.
12 * @ingroup collide
13 */
14 ODE_API void dGeomSetOffsetRotation (dGeomID geom, const dMatrix3 R);
設(shè)置幾何體相對于其物體的局部旋轉(zhuǎn)平移矩陣
1 /**
2 * @brief Set the offset position of a geom from its body.
3 *
4 * Sets the geom's positional offset to move it to the new world
5 * coordinates.
6 * After this call, the geom will be at the world position passed in,
7 * and the offset will be the difference from the current body position.
8 * The geom must be attached to a body.
9 * If the geom did not have an offset, it is automatically created.
10 *
11 * @param geom the geom to set.
12 * @param x the new X coordinate.
13 * @param y the new Y coordinate.
14 * @param z the new Z coordinate.
15 * @ingroup collide
16 */
17 ODE_API void dGeomSetOffsetWorldPosition (dGeomID geom, dReal x, dReal y, dReal z);
生成,設(shè)置幾何對象的函數(shù):
1 ODE_API dGeomID dCreateSphere (dSpaceID space, dReal radius);
2 ODE_API void dGeomSphereSetRadius (dGeomID sphere, dReal radius);
3 ODE_API dReal dGeomSphereGetRadius (dGeomID sphere);
4 ODE_API dGeomID dCreateBox (dSpaceID space, dReal lx, dReal ly, dReal lz);
5 ODE_API void dGeomBoxSetLengths (dGeomID box, dReal lx, dReal ly, dReal lz);
6 ODE_API void dGeomBoxGetLengths (dGeomID box, dVector3 result);
7 ODE_API dGeomID dCreateCapsule (dSpaceID space, dReal radius, dReal length);
8 ODE_API void dGeomCapsuleSetParams (dGeomID ccylinder, dReal radius, dReal length);
9 ODE_API void dGeomCapsuleGetParams (dGeomID ccylinder, dReal *radius, dReal *length);
10 ODE_API dReal dGeomCapsulePointDepth (dGeomID ccylinder, dReal x, dReal y, dReal z);
11 ODE_API dGeomID dCreateCylinder (dSpaceID space, dReal radius, dReal length);
12 ODE_API void dGeomCylinderSetParams (dGeomID cylinder, dReal radius, dReal length);
13 ODE_API void dGeomCylinderGetParams (dGeomID cylinder, dReal *radius, dReal *length);
14 ODE_API dGeomID dCreateRay (dSpaceID space, dReal length);
15 ODE_API void dGeomRaySetLength (dGeomID ray, dReal length);
16 ODE_API dReal dGeomRayGetLength (dGeomID ray);
17 ODE_API void dGeomRaySet (dGeomID ray, dReal px, dReal py, dReal pz,
18 dReal dx, dReal dy, dReal dz);
19 ODE_API void dGeomRayGet (dGeomID ray, dVector3 start, dVector3 dir);