Hey all. I'm trying to program a nice third person camera, and it's causing me a lot of grief to say the least. Right now I'm trying to devise a function that will allow me to calculate the TILT value that my camera needs to have in order to always be pointing at the player. I can't use a fixed value because the camera is elevated above the player and I want it to rotate downward to show him, even when the player's back is right up against a wall (in other words, the value of "a" below can change). I thought that this could be acheived using trig functions, however it's giving me problems.
My basic concept behind my code is as follows.
Based on this idea,
tan(x) = a/b, and atan(a/b) = x. I thought this would allow me to determine the value of x. However my camera is tilting in weird ways and producing undesirable results.
My code is
Code:
vec_set(m_cam_pos,camera.x);
vec_set(cam_play_pos,player.x);
m_cam_pos.z = 0; // so that Z isn't figured in to the vec_dist calculation
cam_play_pos.z = 0;
m_cam_dist = vec_dist(m_cam_pos,cam_play_pos); // determining the value of "a"
temp = (m_cam_dist)/(abs(camera.z-player.z));
camera.tilt = -atan(temp); // to determine the angle that the player is at from the camera (negative because the camera points downward)
Does anyone see anything wrong with this code or anything wrong with my math?
Thanks a lot guys. God bless.