Time for action — controlling Sinbad

We use the previous code and add this code where we need, as we did before. We will create a WASD control for Sinbad with the following code:

  1. Replace the line where we translate the node in the FrameListener with a zero vector called translate:
    Ogre::Vector3 translate(0,0,0);
    
  2. Then add the following keyboard query after the escape query:
    if(_key->isKeyDown(OIS::KC_W))
    {
    translate += Ogre::Vector3(0,0,-10);
    }
    
  3. Now add the code to the other three keys. It is basically the same, only the key code and the direction of the vector changes:
    if(_key->isKeyDown(OIS::KC_S))
    {
    translate += Ogre::Vector3(0,0,10);
    }
    if(_key->isKeyDown(OIS::KC_A))
    {
    translate += Ogre::Vector3(-10,0,0);
    }
    if(_key->isKeyDown(OIS::KC_D))
    {
    translate += Ogre::Vector3(10,0,0);
    }
    
  4. Now use the translate vector to translate the model, and keep in mind to use time-based and not frame-based movement:
    _node->translate(translate*evt.timeSinceLastFrame);
    
  5. Compile and run the application, and you should be able to control Sinbad with the WASD keys
    Time for action — controlling SinbadSinbadSinbadcontrolling

What just happened?

We added a basic movement control using the WASD keys. We queried all four keys and built the accumulative movement vector. We then applied this vector to the model using time-based movement.

Have a go hero — using a speed factor for movement

One downside of this approach is that when we want to change the movement speed of the model, we have to modify four vectors. A better way would be to use the vectors only to indicate the movement direction and use a float variable as the speed factor and multiply the translate vector by it. Change the code to use a movement-speed variable.

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

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