本例子是使用蓋莫游戲引擎+ode做的第三簡單demo
這是對關節(jié)合頁的測試代碼:
1 //! 2010.03.05
2 /////////////////////////////////////////////////////
3 /// 蓋莫游戲引擎蓋莫引擎物理場景測試3
4 /////////////////////////////////////////////////////
5 #include <GEngine/Gaimo.hpp>
6 using namespace std;
7
8 //! 盒子大小和質(zhì)量
9 #define SIDE (8.0f)
10 #define MASS (1.0)
11 //! 盒子紋理
12 int cubeid = 0;
13
14 core::PhysicsEngine<dWorldID,dSpaceID,dJointGroupID,dBodyID,dGeomID> engine;
15 core::PhysicsBody<dBodyID,dGeomID,1> object[2];
16 dJointID hinge;
17 const std::string name[] = {"b1","b2"};
18
19 //! 初始化物理場景
20 void Init();
21 //! 物理場景更新
22 void PhysicsLoop();
23
24 int main(int argc, char **argv)
25 {
26 Init();
27
28 //! 初始化引擎設備并得到設備指針
29 core::Device* device = core::InitDevice("蓋莫引擎物理場景測試3");
30 //! 得到引擎場景指針
31 core::RefPtr<core::SceneManager> scenemanager = device->GetSceneManager();
32 //! 得到引擎資源指針
33 core::ResourceManager* resourcemanager = device->GetResourceManager();
34
35 //! 得到logo圖形和其紋理
36 core::RefPtr<core::Image> box = resourcemanager->GetImage("box","..\\image/logo.jpg");
37 core::RefPtr<core::Texture> boxtexture = resourcemanager->GetTexture("logo",box);
38 boxtexture->Bind();
39 cubeid = boxtexture->GetTextureId();
40
41 //! 獲取全局攝像機
42 core::RefPtr<core::Camera> camera = scenemanager->GetGlobalCamera(Vector3f(30,30,30),
43 Vector3f(0,0,0),
44 Vector3f(0,1,0));
45 camera->SetViewport(0,0,640,480);
46 camera->SetPerspective(50.0f,640.0f/480.0f,0.1f,1000.0f);
47 glClearDepth(1.0f);
48 glEnable(GL_DEPTH_TEST);
49 glShadeModel(GL_SMOOTH);
50 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
51
52 BEGIN_LOOP(device)
53 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54 glLoadIdentity();
55 core::Render::SetClearColor(core::Color::Blue);
56 camera->SetPerspective(45.0f,640.0f/480.0f,0.1f,1000.0f);
57 camera->Render();
58 PhysicsLoop();
59 END_LOOP(device)
60
61 device->Close();
62 device->Drop();
63
64 return 0;
65 }
66
67 //! 初始化物理場景
68 void Init()
69 {
70 //! 設置調(diào)整盒子質(zhì)量
71 dMass m;
72 dMassSetBox(&m,1,SIDE,SIDE,SIDE);
73 dMassAdjust(&m,MASS);
74 //! 從軸和角度獲取四元數(shù)
75 dQuaternion q;
76 dQFromAxisAndAngle(q,1,1,0,0.25*M_PI);
77 //! 設置盒子1物理參數(shù)
78 object[0].body = engine.GetBody(name[0],Vector3f(0.5*SIDE,0.5*SIDE,1));
79 dBodySetMass (object[0].body,&m);
80 dBodySetQuaternion (object[0].body,q);
81 //! 設置盒子2物理參數(shù)
82 object[1].body = engine.GetBody(name[1],Vector3f(-0.5*SIDE,-0.5*SIDE,1));
83 dBodySetMass (object[1].body,&m);
84 dBodySetQuaternion(object[1].body,q);
85 //! 生成合頁關節(jié)并設置參數(shù)
86 hinge = dJointCreateHinge(engine.GetWorld(),0);
87 dJointAttach (hinge,object[0].body,object[1].body);
88 dJointSetHingeAnchor(hinge,0,0,1);
89 dJointSetHingeAxis(hinge,1,-1,1.41421356);
90 }
91
92 //! 物理場景更新
93 void PhysicsLoop()
94 {
95 //! 角阻尼常量
96 const dReal kd = -0.3;
97
98 static dReal a=0;
99 const dReal *w = dBodyGetAngularVel(object[0].body);
100 dBodyAddTorque (object[0].body,kd*w[0],kd*w[1]+0.15*cos(a),kd*w[2]+0.15*sin(a));
101 dWorldStep(engine.GetWorld(),0.05);
102 a += 0.01;
103
104 dReal sides1[3] = {SIDE,SIDE,SIDE};
105 dReal sides2[3] = {SIDE,SIDE,SIDE*0.8f};
106 const dReal *pos,*mat;
107 pos = dBodyGetPosition(object[0].body);
108 mat = dBodyGetRotation(object[0].body);
109 core::Render::RenderCube(cubeid,(float*)pos,(float*)mat,(float*)sides1);
110 pos = dBodyGetPosition(object[1].body);
111 mat = dBodyGetRotation(object[1].body);
112 core::Render::RenderCube(cubeid,(float*)pos,(float*)mat,(float*)sides2);
113 }
貼圖為:
1.

2.