Reputation: 413
I've built a lookAt camera in OpenGL 2.0 (fixed pipeline). My camera keeps track of its position, lookat point and its up vector. Until now I've used the following code to yaw the camera by some amount:
void Camera::yaw(float amount)
{
Vector3 rightVec = getRight();
lookAt.z += rightVec.z * amount;
lookAt.x += rightVec.x * amount;
}
This method seems to work fine when I don't care exactly how much I want to yaw my camera, but now I want to bind the camera to a robot's head, so that when the robot rotates its head the camera yaws accordingly. The way I change the robot's head angle is by keeping its amount of rotation in degrees and just add to that number and change its rotation using glRotatef. so now In order to align my camera and robot I need my camera yaw() function to accept the amount of degrees I want to add and from that calculate the new lookAt values. I tried to figure out the math on my own but I couldn't find a solution that worked right. how can I change my current yaw function to work with degrees delta instead of random ammount.
Upvotes: 2
Views: 2042
Reputation: 22167
What you are facing here is the general problem of rotation one point (lookAt) around another point (position) by a specific angle. This can be done by first translating the problem to the origin, rotating around the origin and then translating back:
lookAt = translate(center) * rotate_y(angle) * translate(-center) * lookAt
(assuming that translate and rotate_y give the corresponding translation/rotation matrices).
When multiplying everthing together, one will get something like this:
center.x + (lookAt.x - center.x) * cos(angle) + (lookAt.z - center.z) * sin(angle)
lookAt = [ lookAt.y ]
center.z + (lookAt.z - center.z) * cos(angle) + (center.x - lookAt.x) * sin(angle)
Note that all calculation assumes (as your previouse also does), that the up vector = [0,1,0].
Upvotes: 2