Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
本例子是使用盖莫游戏引擎+ode做的第三简单demo
这是对关节合页的测试代码:
  1 //! 2010.03.05
  2 /////////////////////////////////////////////////////
  3 /// 盖莫游戏引擎盖莫引擎物理场景测试3
  4 /////////////////////////////////////////////////////  
  5 #include <GEngine/Gaimo.hpp>
  6 using namespace std;
  7 
  8 //! 盒子大小和质量 
  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       //! 设置调整盒子质量 
 71       dMass m;
 72       dMassSetBox(&m,1,SIDE,SIDE,SIDE);
 73       dMassAdjust(&m,MASS);
 74       //! 从轴和角度获取四元数 
 75       dQuaternion q;
 76       dQFromAxisAndAngle(q,1,1,0,0.25*M_PI);
 77       //! 设置盒子1物理参数 
 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物理参数 
 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       //! 生成合页关节并设置参数 
 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 *= 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.
posted on 2010-03-05 11:28 ccsdu2009 阅读(250) 评论(0)  编辑 收藏 引用 所属分类: Game引擎

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理