经典的OGRE机器人的例子(通过例子总结,包含四元数和动画等用法)

该教程介绍了如何使用OGRE库创建一个机器人实体,使其沿着预定义的路径移动,涉及四元数旋转和动画。通过创建SceneNode、Entity和deque来管理行走路径,同时详细讲解了如何更新动画状态和调整机器人朝向。教程以代码示例为主,解释了如何处理180度旋转和避免除以零错误。
摘要由CSDN通过智能技术生成

介绍

这个教程包含了如何使用一个Entity,移动它,并让它在预先定义的点间移动,也包含了四元数旋转的基础

通过如何让一个实体朝着他移动的方向。 你可以慢慢增加代码到工程并观看生成后的结果。

 

开始

首先,给这个demo创建一个新工程, 并增加代码到源文件。

 

#include "ExampleApplication.h"

 

#include <deque>

using namespace std;

 

class MoveDemoListener : public ExampleFrameListener

{

public:

 

    MoveDemoListener(RenderWindow* win, Camera* cam, SceneNode *sn,

    Entity *ent, deque<Vector3> &walk)

    : ExampleFrameListener(win, cam, false, false), mNode(sn),

                            mEntity(ent), mWalkList( walk )

    {

    } // MoveDemoListener

 

    /* This function is called to start the object moving to the next position

    in mWalkList.

    */

    bool nextLocation( )

    {

        return true;

    } // nextLocation( )

 

    bool frameStarted(const FrameEvent &evt)

    {

        return ExampleFrameListener::frameStarted(evt);

    }

protected:

    Real mDistance; // The distance the object has left to travel

    Vector3 mDirection; // The direction the object is moving

    Vector3 mDestination; // The destination the object is moving towards

 

    AnimationState *mAnimationState; // The current animation state of the object

 

    Entity *mEntity; // The Entity we are animating

    SceneNode *mNode; // The SceneNode that the Entity is attached to

    std::deque<Vector3> mWalkList; // The list of points we are walking to

 

    Real mWalkSpeed; // The speed at which the object is moving

};

 

 

class MoveDemoApplication : public ExampleApplication

{

protected:

public:

    MoveDemoApplication()

    {

    }

 

    ~MoveDemoApplication()

    {

    }

protected:

    Entity *mEntity; // The entity of the object we are animating

    SceneNode *mNode; // The SceneNode of the object we are moving

    std::deque<Vector3> mWalkList; // A deque containing the waypoints

 

    void createScene(void)

    {

    }

 

    void createFrameListener(void)

    {

        mFrameListener= new MoveDemoListener(mWindow, mCamera,

                        mNode, mEntity, mWalkList);

        mFrameListener->showDebugOverlay(true);

        mRoot->addFrameListener(mFrameListener);

    }

 

};

 

 

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32

#define WIN32_LEAN_AND_MEAN

#include "windows.h"

 

 

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )

#else

int main(int argc, char **argv)

#endif

{

    // Create application object

    MoveDemoApplication app;

 

    try {

        app.go();

    } catch( Exception& e ) {

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32

        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!",

        MB_OK | MB_ICONERROR | MB_TASKMODAL);

#else

        fprintf(stderr, "An exception has occured: %s\n",

        e.getFullDescription().c_str());

#endif

    }

 

return 0;

}

 

对代码的分析: 

设置这个场景

在开始前,注意我们已经在MoveDemoApplication里定义了三个变量。mEntity保存了我们创建的entity。mNode 保存了我们创建的节点 。mWalkList 包含了我们希望移动到的所有点。

接下来到 MoveDemoApplication::createScene 函数里并增加下面的代码, 我们先要设置环境光来让我们在屏幕上看到场景里的所有对象:

     // Set the default lighting.

    mSceneMgr->setAmbientLight( ColourValue( 1.0f 1.0f 1.0f ) );

接下来,我们创建一个机器人到屏幕,好让我们可以播放它。那你就需要创建一个机器人实体,然后创建一个SceneNode让它依附:

    // Create the entity

    mEntity = mSceneMgr->createEntity( "Robot" "robot.mesh" );

    // Create the scene node

    mNode = mSceneMgr->getRootSceneNode( )->

    createChildSceneNode( "RobotNode" Vector3( 0.0f 0.0f 25.0f ) );

    mNode->attachObject( mEntity );

这些都是非常基础的,因此我不再相继解释。在下个代码块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值