Rotating the camera toward a target

Posted By: Logan

Rotating the camera toward a target - 01/24/11 19:31

Hi, this is a completely n00b question--one I've solved before, but I just can't figure it out right now. I want to rotate the camera smoothly toward a target. I know how to compute the angles to the target, but I am having trouble figuring out how to then rotate the camera smoothly toward those angles. The camera will likely change both its pan AND tilt (ideally simultaneously)... and it is not guaranteed what direction. That is to say, the object to be rotated toward may be up and to the left of the camera, or down and to the right, or whatever. So I want the camera to figure out what direction it needs to go, and then rotate there, ideally getting there in about 10 frames.

Here is what I have so far:
Code:
vec_set(turnvector,focus_ent.x); 
vec_sub(turnvector,camera.x);
vec_to_angle(turnangle,turnvector);
// now I need to smoothly rotate the camera to turnangle



I would be really appreciative of any help, even if it's just a nudge in the right direction.

Thanks in advance.

Logan
Posted By: Superku

Re: Rotating the camera toward a target - 01/24/11 20:12

var smoothing_factor = 0.1;
...

camera.pan += ang(turnangle-camera.pan)*smoothing_factor*time_step;
camera.tilt += ang(turnangle-camera.tilt)*smoothing_factor*time_step;

EDIT: You can limit the speed with clamp, too:

var smoothing_factor = 0.1;
var smoothing_limit = 10;
...

camera.pan += clamp(ang(turnangle-camera.pan)*smoothing_factor,-smoothing_limit,smoothing_limit)*time_step;
camera.tilt += clamp(ang(turnangle-camera.tilt)*smoothing_factor,-smoothing_limit,smoothing_limit)*time_step;
Posted By: Logan

Re: Rotating the camera toward a target - 01/24/11 23:05

Thanks a ton Superku. It looks great. And it's so sleek! 2 lines!
© 2024 lite-C Forums