OgreBullet 的使用

最近做实训,用到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 一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值