游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2324|回复: 0

使用盖莫游戏引擎做的简单物理场景测试3

[复制链接]

50

主题

69

帖子

69

积分

注册会员

Rank: 2

积分
69
发表于 2010-3-5 11:34:00 | 显示全部楼层 |阅读模式

本例子是使用盖莫游戏引擎+ode做的第三简单demo
这是对关节合页的测试代码:
//! 2010.03.05
/////////////////////////////////////////////////////
/// 盖莫游戏引擎盖莫引擎物理场景测试3
/////////////////////////////////////////////////////  
#include <GEngine/Gaimo.hpp>
using namespace std;

//! 盒子大小和质量
#define SIDE (8.0f)     
#define MASS (1.0)
//! 盒子纹理
int  cubeid = 0;  

core:hysicsEngine<dWorldID,dSpaceID,dJointGroupID,dBodyID,dGeomID> engine;
core::PhysicsBody<dBodyID,dGeomID,1> object[2];
dJointID hinge;
const std::string name[] = {"b1","b2"};

//! 初始化物理场景
void Init();
//! 物理场景更新
void PhysicsLoop();  

int main(int argc, char **argv)
{   
    Init();
   
    //! 初始化引擎设备并得到设备指针
    core:evice* device = core::InitDevice("盖莫引擎物理场景测试3");
    //! 得到引擎场景指针
    core::RefPtr<core::SceneManager> scenemanager = device->GetSceneManager();
    //! 得到引擎资源指针
    core::ResourceManager* resourcemanager = device->GetResourceManager();
   
    //! 得到logo图形和其纹理
    core::RefPtr<core::Image>   box = resourcemanager->GetImage("box","..\\image/logo.jpg");
    core::RefPtr<core::Texture> boxtexture = resourcemanager->GetTexture("logo",box);
    boxtexture->Bind();
    cubeid = boxtexture->GetTextureId();
   
    //! 获取全局摄像机
    core::RefPtr<core::Camera> camera = scenemanager->GetGlobalCamera(Vector3f(30,30,30),
                                                                      Vector3f(0,0,0),
                                                                      Vector3f(0,1,0));
    camera->SetViewport(0,0,640,480);  
    camera->SetPerspective(50.0f,640.0f/480.0f,0.1f,1000.0f);                                                                  
    glClearDepth(1.0f);                                                                                
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);                                    
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   

    BEGIN_LOOP(device)
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);            
       glLoadIdentity();
       core::Render::SetClearColor(core::Color::Blue);
       camera->SetPerspective(45.0f,640.0f/480.0f,0.1f,1000.0f);
       camera->Render();  
       PhysicsLoop();
    END_LOOP(device)
   
    device->Close();
    device->Drop();

    return 0;
}

//! 初始化物理场景
void Init()
{
      //! 设置调整盒子质量
      dMass m;
      dMassSetBox(&m,1,SIDE,SIDE,SIDE);
      dMassAdjust(&m,MASS);
      //! 从轴和角度获取四元数
      dQuaternion q;
      dQFromAxisAndAngle(q,1,1,0,0.25*M_PI);
      //! 设置盒子1物理参数
      object[0].body = engine.GetBody(name[0],Vector3f(0.5*SIDE,0.5*SIDE,1));
      dBodySetMass (object[0].body,&m);
      dBodySetQuaternion (object[0].body,q);
      //! 设置盒子2物理参数
      object[1].body = engine.GetBody(name[1],Vector3f(-0.5*SIDE,-0.5*SIDE,1));
      dBodySetMass (object[1].body,&m);
      dBodySetQuaternion(object[1].body,q);
      //! 生成合页关节并设置参数
      hinge = dJointCreateHinge(engine.GetWorld(),0);
      dJointAttach (hinge,object[0].body,object[1].body);
      dJointSetHingeAnchor(hinge,0,0,1);
      dJointSetHingeAxis(hinge,1,-1,1.41421356);      
}

//! 物理场景更新
void PhysicsLoop()
{   
    //! 角阻尼常量   
    const dReal kd = -0.3;

    static dReal a=0;
    const dReal *w = dBodyGetAngularVel(object[0].body);
    dBodyAddTorque (object[0].body,kd*w[0],kd*w[1]+0.15*cos(a),kd*w[2]+0.15*sin(a));
    dWorldStep(engine.GetWorld(),0.05);
    a += 0.01;

    dReal sides1[3] = {SIDE,SIDE,SIDE};
    dReal sides2[3] = {SIDE,SIDE,SIDE*0.8f};
    const dReal *pos,*mat;
    pos = dBodyGetPosition(object[0].body);
    mat = dBodyGetRotation(object[0].body);
    core::Render::RenderCube(cubeid,(float*)pos,(float*)mat,(float*)sides1);                 
    pos = dBodyGetPosition(object[1].body);
    mat = dBodyGetRotation(object[1].body);
    core::Render::RenderCube(cubeid,(float*)pos,(float*)mat,(float*)sides2);         
}


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-5-14 00:49

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表