Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need some help in 'camera maths'. I have a birds eye view of two characters. One character is static and the other can move. I would like the camera to always show both characters in full and, in order to simulate 'zooming out', I'm uniform-scaling the objects. The static character is always at the top of the screen and so the 'camera' can't move along the Y-axis (otherwise the static character would not be shown in whole). However, it can move along the X-axis as far as possible, until the screen reaches the side of the static character. How can I calculate the minimum distance the camera should zoom out in order to ensure that both objects are always on screen?

I hope that makes sense, but I'd be happy to try and clarify it if need be. Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I'll take a stab here... I think what you want is:

tan( 1/2 * field_of_view ) * ( 1/2 * distance_between_objects)

Since you can't see my whiteboard, I'll try to describe the idea. Take your camera and your two objects, and draw a triangle between them. Now draw a line from your camera to the side of the triangle opposing it (and perpendicular to it). You now have two right triangles. You know the angle at your camera (1/2 * field_of_view) and the length of the line segment opposing it (1/2 * distance_between_objects). The tangent function, given the angle, will tell you the relationship between the length of your opposite side and your adjacent side, the first of which you know.

The result is the distance from the line between the two objects (probably your ground plane) and where your camera should be. You probably want to add some "padding" to this so your objects aren't on the very edge of your view.

share|improve this answer
    
Thanks, that seems to work well. –  Skoder Apr 8 '11 at 20:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.