Now we are going to add interactivity to the scene by adding the user control to the movements of the model.
FrameListener
needs two new members: one pointer to the node we want to move, and one float indicating the movement speed:float _WalkingSpeed; Ogre::SceneNode* _node;
MyFrameListener(Ogre::RenderWindow* win,Ogre::Camera* cam,Ogre::Viewport* viewport,Ogre::SceneNode* node)
50:
_WalkingSpeed = 50.0f; _node = node;
frameStarted
function we need two new variables, which will hold the rotation and the translation the user wants to apply to the node:Ogre::Vector3 SinbadTranslate(0,0,0); float _rotation = 0.0f;
if(_Keyboard->isKeyDown(OIS::KC_UP)) { SinbadTranslate += Ogre::Vector3(0,0,-1); _rotation = 3.14f; } if(_Keyboard->isKeyDown(OIS::KC_DOWN)) { SinbadTranslate += Ogre::Vector3(0,0,1); _rotation = 0.0f; } if(_Keyboard->isKeyDown(OIS::KC_LEFT)) { SinbadTranslate += Ogre::Vector3(-1,0,0); _rotation = -1.57f; } if(_Keyboard->isKeyDown(OIS::KC_RIGHT)) { SinbadTranslate += Ogre::Vector3(1,0,0); _rotation = 1.57f; }
_node->translate(SinbadTranslate * evt.timeSinceLastFrame * _WalkingSpeed); _node->resetOrientation(); _node->yaw(Ogre::Radian(_rotation));
Ogre::SceneNode* _SinbadNode;
FrameListener
instantiation needs this pointer:_listener = new MyFrameListener(window,camera,viewport,_SinbadNode);
createScene
function needs to use this pointer to create and store the node of the entity we want to move; modify the code in the function accordingly:_SinbadNode = _sceneManager->getRootSceneNode()->createChildSceneNode(); _SinbadNode->attachObject(sinbadEnt);
3.135.198.174