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:
FrameListener
with a zero vector called translate:
Ogre::Vector3 translate(0,0,0);
if(_key->isKeyDown(OIS::KC_W)) { translate += Ogre::Vector3(0,0,-10); }
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); }
_node->translate(translate*evt.timeSinceLastFrame);
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.
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.
18.224.109.21