This section describe how can move camera in the scene and in the other hand control it,in our sample code we also use the keyboard class for input device.
This code continues the previous code is called Hello World
To rotate the camera by mouse, easily we can use the below function:
pCamera->AutoRotateByMouse();
note that this function automatically rotate the target position of camera by move the mouse as input deviceĀ and global camera position in the scene was fix.
for use the keyboard we need the pointer of it’s class then declare it Where the rest of pointers defines carry on:
io::keyboard* pKeyboard;
now for initialize the pointer address ,we write this line:
pKeyboard = new io::Keyboard();
For we know that the user press the which keys, can use the KeyDown function of keyboard Class like this:
pKeyboard->KeyDown( key code );
key code is one byte that define for every key on the keyboards and OS return it when every key pressed,don’t worry all of those key was defined in the one enum calledĀ io::KEYS and you can use it like this:
pKeyboard->keyDown( io::EK_ESCAPE );
now if the ESC key in keyboard pressed,this function return true(or 1) else return false(or 0)
For move the global camera position in the scene to left and right we can use the MoveRL function of camera Class:
pCamera->MoveRL( speed );
speed parameter is the float,and describe the speed of changing the position of camera, if use Negative number camera move to left otherwise move to right.
also for move the global camera position to Back and Front we can use the MoveBF function of camera class:
pCamera->MoveBF( speed );
like previous function also this parameter is float and describe the speed of changing the camera position,if use Negative number camera move to back otherwise move to front.
In the blow code you can see how control the position of the camera by keyboard , and use it’s function:
while( pDevice->Run() )
{
pSceneManager->RenderAll();
if( pKeyboard->KeyDown(io::EK_a) )
pCamera->MoveRL( -1.0f );
else if( pKeyboard->KeyDown(io::EK_d) )
pCamera->MoveRL( 1.0f );
else if( pKeyboard->KeyDown(io::EK_w) )
pCamera->MoveBF( 1.0f );
else if( pKeyboard->KeyDown(io::EK_s) )
pCamera->MoveBF( -1.0f );
else if( pKeyboard->KeyDown(io::EK_ESCAPE) )
pCamera->AutoRotateByMouse(false);
pRenderer->EndRendering();
}
in line 22 and 23 we check the keyboard and if user press the ESC key on keyboard,disable the rotate camera by mouse automatically