This tutorial was written for me by Imran Khan (firstname.lastname@example.org,
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.
First of all we will define a method ' Move_Camera ' . Which will use to move the camera forward and backward.
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.
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" .
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...
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.
First we get the the center of the window from these lines :
int mid_x = wndWidth >> 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;
The we apply certain limit to the rotation around the x-axis.
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.
Now you have Full Command , You can look around, move around... Enjoy !
|Correspondence about this page|
Book Shop - Further reading.
Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.
Commercial Software Shop
Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.
This site may have errors. Don't use for critical systems.