Time for action — adding animation

Our model can move but it isn't animated yet, let's change this.

  1. The FrameListener needs two animation states:
    Ogre::AnimationState* _aniState;
    Ogre::AnimationState* _aniStateTop;
    
  2. To get the animation states in the constructor, we need a pointer to the entity:
    MyFrameListener(Ogre::RenderWindow* win,Ogre::Camera* cam,Ogre::Viewport* viewport,Ogre::SceneNode* node,Ogre::Entity* ent)
    
  3. With this pointer we can retrieve the AnimationState and save them for later use:
    _aniState = ent->getAnimationState("RunBase");
    _aniState->setLoop(false);
    _aniStateTop = ent->getAnimationState(«RunTop»);
    _aniStateTop->setLoop(false);
    
  4. Now that we have the AnimationState, we need to have a flag in the frameStarted function, which tells us whether or not the entity walked this frame. We add this flag into the if conditions that query the keyboard state:
    bool walked = false;
    if(_Keyboard->isKeyDown(OIS::KC_UP))
    {
    SinbadTranslate += Ogre::Vector3(0,0,-1);
    _rotation = 3.14f;
    walked = true;
    }
    if(_Keyboard->isKeyDown(OIS::KC_DOWN))
    {
    SinbadTranslate += Ogre::Vector3(0,0,1);
    _rotation = 0.0f;
    walked = true;
    }
    if(_Keyboard->isKeyDown(OIS::KC_LEFT))
    {
    SinbadTranslate += Ogre::Vector3(-1,0,0);
    _rotation = -1.57f;
    walked = true;
    }
    if(_Keyboard->isKeyDown(OIS::KC_RIGHT))
    {
    SinbadTranslate += Ogre::Vector3(1,0,0);
    _rotation = 1.57f;
    walked = true;
    }
    
  5. If the model moves, we enable the animation; if the animation has ended, we loop it:
    if(walked)
    {
    _aniState->setEnabled(true);
    _aniStateTop->setEnabled(true);
    if(_aniState->hasEnded())
    {
    _aniState->setTimePosition(0.0f);
    }
    if(_aniStateTop->hasEnded())
    {
    _aniStateTop->setTimePosition(0.0f);
    }
    }
    
  6. If the model didn't move, we disable the animation and set it to the start position:
    else
    {
    _aniState->setTimePosition(0.0f);
    _aniState->setEnabled(false);
    _aniStateTop->setTimePosition(0.0f);
    _aniStateTop->setEnabled(false);
    }
    
  7. In each frame, we need to add the passed time to the animation; otherwise, it wouldn't move:
    _aniState->addTime(evt.timeSinceLastFrame);
    _aniStateTop->addTime(evt.timeSinceLastFrame);
    
  8. The application now also needs a pointer to the entity:
    Ogre::Entity* _SinbadEnt;
    
  9. We use this pointer while instantiating the FrameListener:
    _listener = new MyFrameListener(window,camera,viewport,_SinbadNode,_SinbadEnt);
    
  10. And, of course, while creating the entity:
    _SinbadEnt = _sceneManager->createEntity("Sinbad.mesh");
    
  11. Compile and run the application. Now the model should be animated when it moves:
    Time for action — adding animation

What just happened?

We added animation to our model, which is only enabled when the model is moved.

Have a go hero — looking up what we used

Look up the chapters where we discussed the techniques we used for the last examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.8.91