上QQ阅读APP看书,第一时间看更新
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:
- Replace the line where we translate the node in the
FrameListener
with a zero vector calledtranslate:
Ogre::Vector3 translate(0,0,0);
- Then add the following keyboard query after the escape query:
if(_key->isKeyDown(OIS::KC_W)) { translate += Ogre::Vector3(0,0,-10); }
- 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); }
- 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);
- Compile and run the application, and you should be able to control Sinbad with the WASD keys
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.