OgreBullet 的使用

来源:互联网 发布:刻录软件怎么用 编辑:程序博客网 时间:2024/06/14 22:35

最近做实训,用到Ogre 的引擎。

在渲染方面,Ogre 的性能杠杠的,不用多解释,但是除了渲染,别的模块很多都是空缺的。
so,当我尝试去做物理引擎的时候,发现就像来到了一篇荒原,对,就是荒原。

所幸,前人做了很多的物理引擎与Ogre 的匹配。OgreBullet 就是与Bullet 匹配的东西。
但是这玩意有点历史,项目还是08年的,我使用的是Ogre的1.9 版本了。

简单尝试了一下,OgreBullet 的使用大概如下。
头文件包含和命名空间的声明:

#include <OgreBulletDynamicsRigidBody.h>#include <Shapes/OgreBulletCollisionsStaticPlaneShape.h>#include <Shapes/OgreBulletCollisionsBoxShape.h>using namespace OgreBulletDynamics;using namespace OgreBulletCollisions;

关键的变量:

    DynamicsWorld* world;    std::deque<RigidBody*> bodies; //deque for rigid body    std::deque<CollisionShape*> shapes;

然后初始化一下变量:

world = new DynamicsWorld(scene_mgr, bound, gravity_vector);

刚体碰撞基本遵循两个步骤:建立CollisionShape 、建立RigidBody

以两个函数,记录如下:

    void add_shape(const Plane _g,const String& name = StringUtil::BLANK)    {        CollisionShape* shape = new StaticPlaneCollisionShape(_g.normal,            -50 );                //CollisionShape* shape = new StaticPlaneCollisionShape(Ogre::Vector3(0,1,0), 0);        RigidBody* body = new RigidBody(name ,world);        body->setStaticShape(shape, 0.1 ,0.8);        _add_shape(shape, body);    }    void add_shape(SceneNode* node, ogre_vec3 size)    {        //ToDo        ogre_vec3 position = (_camera->getDerivedPosition() + _camera->getDerivedDirection().normalisedCopy() * 10);        BoxCollisionShape* box_shape = new BoxCollisionShape(size);        RigidBody* rigid_body = new RigidBody("defaultBoxRigid" + StringConverter::toString(box_nums), world);        rigid_body->setShape(             node,            box_shape,            0.6f, // dynamic body restitution            0.6f,   // dynamic body friction            1.0f,   // dynamic bodymass            position,   // starting position of the box            Quaternion(0,0,0,1));   // orientation of the box        rigid_body->setLinearVelocity(            _camera->getDerivedDirection().normalisedCopy() * 7.0f); // shoot speed        _add_shape(box_shape, rigid_body);        ++box_nums;    }

然后在frameStarted 和 frameEnded 中启用模拟:

bool Game_controller::frameStarted(const FrameEvent& fe){    ogrebullet.frame_simulation(fe);    return true;}bool Game_controller::frameEnded(const FrameEvent& fe){    ogrebullet.frame_simulation(fe);    return true;}    void frame_simulation(const FrameEvent& fe)    {        world->stepSimulation(fe.timeSinceLastFrame);    }

我比较不明白的是,析构对象的时候,它会莫名奇妙地崩溃。。。先不管了,真正用的时候,再fixed 一下。

1 0
原创粉丝点击