# Programming - Games - OpenGL and C++

This tutorial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
Copyright (c) 2004 is owned by Martin Baker Keyboard & Mouse Interaction:

In this Section we will learn that how we can use Keyboard and Mouse to interact with the Program. This is one the main and most important thing of any Game. Actually we won't move in a 3D-World , we will use Camera to move around. As we have learned that how to initialize Camera Posisition. Now we will move this camera around on keyboard press and on mouse movement.

First of all we will define a constant name 'cameraspeed '. Which is use to set the speed of the Camera.

 #define CAMERASPEED 0.03f

First of all we will define a method ' Move_Camera ' . Which will use to move the camera forward and backward.

 void Move_Camera(float speed) { tVector3 vVector = mView - mPos; // Get the view vector mPos.x = mPos.x + vVector.x * speed; mPos.z = mPos.z + vVector.z * speed; mView.x = mView.x + vVector.x * speed; mView.z = mView.z + vVector.z * speed; }

First get the View vector by subtracting the Position vector from View vector. Next we just add the values to x and z of Position and View Vector because we want to move only forward and backward.

Now we will define a method 'Rotate_View' method. Which is use to move the Camera View left and right.

 void Rotate_View(float speed) { tVector3 vVector = mView - mPos; mView.z = (float)(mPos.z + sin(speed)*vVector.x + cos(speed)*vVector.z); mView.x = (float)(mPos.x + cos(speed)*vVector.x - sin(speed)*vVector.z); }

First get the View vector by subtracting the Position vector from View vector. Then we apply the formula for the rotation. We do this only to View Vector because we don't want to move the Camera, we just want to rotate the View. Camera is still at its position.

Now we will define a method for Keyboard_Input.. We can also write these lines of code in "WinMain" but it will look complex thats why we will write its seperate method. So that it will more clear and simple to understand. Then we will easly call this function from "WinMain" .

 void Keyboard_Input() { if((GetKeyState(VK_UP) & 0x80)) { Move_Camera( CAMERASPEED); } if((GetKeyState(VK_DOWN) & 0x80)) { Move_Camera(-CAMERASPEED); } if((GetKeyState(VK_LEFT) & 0x80)) { Rotate_View(-CAMERASPEED); } if((GetKeyState(VK_RIGHT) & 0x80)) { Rotate_View( CAMERASPEED); } }

For 'up' and ' Down ' Key we call " Move_Camera " and for ' left ' and ' right ' Key , we will call " Rotate_View ".

Next we will just call this function from " WinMain " method...

 else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { Keyboard_Input(); // here we are calling the method if (active) { DrawGLScene(); SwapBuffers(hDC); }

This is not the full function, but just that part of the code in which we are calling the " KeyBoard_input " method.

Now when u run the program you can move " Forward " & " Backword " . You can Turn Arround. But you can't see up and down. We will do this with Mouse.

Now we will define the funtion " Mouse_Move " as we did for Keyboard_input.

 void Mouse_Move(int wndWidth, int wndHeight) { POINT mousePos; int mid_x = wndWidth >> 1; int mid_y = wndHeight >> 1; float angle_y = 0.0f; float angle_z = 0.0f; GetCursorPos(&mousePos); SetCursorPos(mid_x, mid_y); angle_y = (float)( (mid_x - mousePos.x) ) / 1000; angle_z = (float)( (mid_y - mousePos.y) ) / 1000; mView.y += angle_z * 2; if((mView.y - mPos.y) > 8) mView.y = mPos.y + 8; if((mView.y - mPos.y) <-8) mView.y = mPos.y - 8; Rotate_View(-angle_y); }

First we get the the center of the window from these lines :

int mid_x = wndWidth >> 1;
int mid_y = wndHeight >> 1;

Next we set the Get the Current Cursor Position and then Set the Current Cursor Position to the Center of the screen, Which we have set.

Then we get the direction from the mouse cursor and set a resonable speed.

angle_y = (float)( (mid_x - mousePos.x) ) / 1000;
angle_z = (float)( (mid_y - mousePos.y) ) / 1000;

The we apply certain limit to the rotation around the x-axis.

if((mView.y - mPos.y) > 8) mView.y = mPos.y + 8;
if((mView.y - mPos.y) <-8) mView.y = mPos.y - 8;

and in the end we call Rotate_view Method to rotate the View.

As we have done all the things , we just have to call this "Mouse_Move" Fucntion.

 else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { Keyboard_Input(); // Keyboard Input Mouse_Move(640,480); // mouse movement. if (active) { DrawGLScene(); SwapBuffers(hDC); }

Now you have Full Command , You can look around, move around... Enjoy !