 # Why does this math work?

//This is a rotate function that is passed an angle

cx = sin(angle);
cz = -cos(angle); //->Why negative Cosine?

glMatrixMode(GL_PROJECTION);

gluPerspective(45, ratio, 1, 1000);

gluLookAt(x,y,z, /Camera Position/ x+cx,y+cy,z+cz, 0,1,0); /*Up Vector */

This code works perfectly for rotating the view in a 3D world. I pass in angle to my function and this code updates the view accordingly. I don’t understand why you can take the cosine/sine of the angle when the center is not the origin (0,0,0). I mean, I could be anywhere in my 3D world and this code works. Also, why do you take NEGATIVE cosine. I thought you take the value of cos(theta) to get the “x” value and sine(theta) to get the “z” value?

Does the gluLookAt() translate to the origin, rotate, and then translate back? (behind the scene?)

I just want to understand this fragment.

Any help would be appreciated.

That minus sign before cosine depends on how you want your world coordinate system to look like. Some people prefer a right-handed system, and some a left-handed system, and setup ther environment after their needs. If you go from a left-handed system to a right-handed system, some axis will change sign. And it also depends on what direction you want when the angle is zero. x=sin and z=cos will result in a look-at direction down the positive Z-axis when the angle is zero. You can also use x=cos and z=sin. Then you look down then positive X when the angle is zero. It also depends on what direction you want to rotate when the angle increase. Why YOU use a negatine cosine for Z and positive sine for X is beyond my knowledge, it’s your choise and I don’t know why you did it.

And why you take the sine/cosine directly like that, is becaue you don’t get a specific point in worldspace, you get an offset from the viewpoint you also pass to gluLookAt

And gluLookAt rotates and then translates the origin, or more correctly, the world around the origin.

Thank you

Hey Ace_Man, thanks for mentioning that the minus sign was required there for the 1st person view rotation. I had some trouble with that. How’d you figure out that the Cosine of the angle needed a minus sign?
Keepin it real
-Brian

Well, the minus sign is only required if you want to achieve that particular effect. To figure out how to do, just take a paper and draw the coordinate system, and with a little bit of thinking, you can come up with just about anything you need about movements. It’s just a matter of HOW YOU want the scene to respond on a certain parameter.