• ogre_机器人在10个位置循环走动


    #include <ExampleApplication.h>
    #include <vector>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;

    class MoveDemoListener : public ExampleFrameListener
    {
    protected:
     Vector3 mDirection;
     Vector3 mPosition;
     Real mMove;
     Real mDistance;
     Real mTime;
     AnimationState* mAnimationState;
     Entity* mEntity;
     SceneNode* mNode;
     std::vector<Vector3> mWalkList;
     int mIndex;
     Real mWalkSpeed;
    public:
     MoveDemoListener(RenderWindow* win, Camera* cam, SceneNode* sn, Entity* ent, std::vector<Vector3>& walk)
      : ExampleFrameListener(win, cam, false, false), mNode(sn), mEntity(ent), mWalkList(walk)
     {
      mAnimationState = ent->getAnimationState("Walk");
      mAnimationState->setLoop(true);
      mAnimationState->setEnabled(true);
      mWalkSpeed = 35.0f;
      mIndex = 0;
      int nIndex1 = mIndex, nIndex2 = (mIndex + 1) % 10;
      mDirection = mWalkList[nIndex2] - mWalkList[nIndex1];
      mDistance = mDirection.length();
      mDirection.normalise();
      Vector3 Direction;
      Direction.x = mDirection.z;
      Direction.y = 0.0f;
      Direction.z = -mDirection.x;
      mNode->setDirection(Direction, Ogre::Node::TS_WORLD);
      mTime = 0.0f;
     }

     void nextLocation()
     {
      mIndex = (mIndex + 1) % 10;
      int nIndex1 = mIndex, nIndex2 = (mIndex + 1) % 10;
      mDirection = mWalkList[nIndex2] - mWalkList[nIndex1];
      mDistance = mDirection.length();
      mDirection.normalise();
      Vector3 Direction;
      Direction.x = mDirection.z;
      Direction.y = 0.0f;
      Direction.z = -mDirection.x;
      mNode->setDirection(Direction, Ogre::Node::TS_WORLD);
      mTime = 0.0f;
      Matrix4 matRotation;
      
     }

     bool frameStarted(const FrameEvent& evt)
     {
      mTime += evt.timeSinceLastFrame;
      mMove = mTime * mWalkSpeed;  
      mPosition = mWalkList[mIndex] + mMove * mDirection;
      mPosition.y = 0.0f;
      mNode->setPosition(mPosition);
      if(mMove > mDistance) nextLocation();
      mAnimationState->addTime(evt.timeSinceLastFrame);
      return ExampleFrameListener::frameStarted(evt);
     }
    };

    class MoveDemoApplication : public ExampleApplication
    {
    public:
     MoveDemoApplication()
     {

     }

     ~MoveDemoApplication()
     {

     }
    protected:
     Entity* mEntity;
     SceneNode* mNode;
     std::vector<Vector3> mWalkList;
     void createScene()
     {
      srand(1000);
      char s[32];
      Vector3 v;
      int i;
      for(i = 0; i < 10; i ++)
      {
       mWalkList.push_back(Vector3(rand() % 1000, -10.0f, rand() % 1000));
      }
      v = mWalkList[0];
      v.y = 0.0f;
      mSceneMgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));
      mEntity = mSceneMgr->createEntity("Robot", "robot.mesh");
      mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("RobotNode", v);
      mNode->attachObject(mEntity);
      Entity* ent;
      SceneNode* node;
      for(i = 0; i < 10; i ++)
      {
       sprintf(s, "Knot%d", i + 1);
       ent = mSceneMgr->createEntity(s, "knot.mesh");
       sprintf(s, "Knot%dNode", i + 1);
       node = mSceneMgr->getRootSceneNode()->createChildSceneNode(s, mWalkList[i]);
       node->attachObject(ent);
       node->setScale(0.1f, 0.1f, 0.1f);
      }
      mCamera->setPosition(90.0f, 280.0f, 535.0f);
      mCamera->pitch(Degree(-30.0f));
      mCamera->yaw(Degree(-15.0f));
     }

     void createFrameListener()
     {
      mFrameListener = new MoveDemoListener(mWindow, mCamera, mNode, mEntity, mWalkList);
      mFrameListener->showDebugOverlay(false);
      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
    {
     MoveDemoApplication app;
     try {
      app.go();
     } catch(Exception& e) {
    #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
      MessageBoxA(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;
    }

  • 相关阅读:
    20172302 201720182 《程序设计与数据结构》实验二报告
    20172302 201720182 《程序设计与数据结构》实验一报告
    20172302 《程序设计与数据结构》第五周学习总结
    POJ 1061 青蛙的约会
    hdu 2485 Highways
    UVA 10608
    hdu 1213 how many tables
    java类static成员加载顺寻
    C# virtual,override或者new
    vs无法在WEB服务器上启动调试
  • 原文地址:https://www.cnblogs.com/ketmales/p/2477040.html
Copyright © 2020-2023  润新知