I try to make a camera that can walk on X/Z plane and rotate 360 degrees. I've looked at tutorials but I can't understand them. Most of them use trigonometric functions, like the one below:
// horizontal angle : toward -Z
float horizontalAngle = 3.14f;
// vertical angle : 0, look at the horizon
float verticalAngle = 0.0f;
int xpos, ypos;
horizontalAngle += mouseSpeed * deltaTime * float(1024/2 - xpos );
verticalAngle += mouseSpeed * deltaTime * float( 768/2 - ypos );
cos(verticalAngle) * sin(horizontalAngle),
cos(verticalAngle) * cos(horizontalAngle)
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
cos(horizontalAngle - 3.14f/2.0f)
glm::vec3 up = glm::cross( right, direction );
ViewMatrix = glm::lookAt(
position, // Camera is here
position+direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
Why is trigonometry used in the code,how do I create a camera that can freely walk and look around?
You may want to learn spherical coordinates and the mathematics behind rotation matrices. Graphics programming will seem nearly impossible without a solid understanding of trigonometry, linear algebra, and vector analysis. In your case, the camera model is easier to describe using spherical coordinates but it needs to be transformed into rectangular coordinates before you can use it in your OpenGL scene.
For the view matrix, you need a direction vector, a right vector, and the up vector. The direction vector is found by converting the horizontal and vertical angles from spherical coordinates to rectangular. The other "trigonometric" code computes the right vector. It takes the horizontal angle and rotates it by 90 degrees, which is the same thing as rotating pi / 2 radians. Taking the sin of the horizontal for x, y equal to 0, and the cos of the horizontal for z, gives the required coordinates. Finally, the up vector is just the cross product of the right and direction vectors. This gives the three vectors needed for constructing the view matrix which will update the camera.