Guys, If you have played CS 1.6, I want to make my player to look around when I move the mouse up/down/left/right like in a fps game. I found the code, I spent all day trying to understand it and I still can't and I was wondering if someone can explain it to me.
Imagine that my camera is always at point ( 0, 0, 0 ). I need to set the point where the camera looks at. And I need that point to be on ( to lie on ) a sphere with radius = 1.0f. And when I move the mouse, I need the lookat point to be changed, but to lie on the sphere again. So basically I move my lookat point around a sphere with the mouse, the bigger the sphere is, the less sensitivity the mouse has.( and my coordinates are on the center on the sphere, of course). First I tried to do just left/right mouse movement, without up/down, and this was easy, just set the lookat point with ( cos(angle), y, sin(angle) ). But when I need to progress from circle to sphere, sh*t gets real, because when I move up/down, I mess up my left/right coordinates.
The code I found is this:
glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
I just need to sum this direction vector with my currentPosition vector and to use it as my lookAt point and the joke is that it actually works. It is done like this:
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)
);
If you got anything from what I said, I will be really grateful if you somehow manage to tell me why this really works, because this logic is just beyond me. :wacko:
EDIT: Ok, I've made some progress in trying to understand this crazy logic. Basically, when I am looking straight "forward" , "y" is 0. And as I move the mouse UP and look to the skies, the y gets bigger, for example: y = 0 goes to y = 0.677465. And as the "y" gets bigger, the x and z get smaller by the same ratio, that's why "x" and "z" coordinates depend on the "y" coordinate. And that's why there is cos(verticalAngle) in both x and y. But now I don't understand why the cos(verAngle) is multiplied by the sin(horAngle) ( I'm talking about "x" here )???