automatic camera update @ loading objects

Good afternoon,

I am writing an application for loading models at run-time and position the camera in a TOP view…so I wonder if anyone can address me to how can I compute the position of the camera automatically in order to have my objects fitting the viewport and how can I change the field of view in order to let the object not clipped off.

Many thanks, Giancarlo

The field of view and the distance from the camera to the object are the parameters that will determine the object’s apparent size on screen, so you can adjust one or both of these to get the desired effect. (That is, you could place the camera a fixed height above the object and adjust the field of view, or use a fixed field of view and adjust the height, or some combination of the two.)

You should be able to get an approximate fit (which will most likely be close enough) by setting the height and/or field of view based on the radius of the object’s bounding sphere. This essentially reduces to a simple trig problem. For example, given a fixed height, I believe the field of view can be computed as (pseudocode):

float x = sqrt(distance * distance - radius * radius);
float fieldOfView = atan2(radius, x) * 2;

That’s off the top of my head and may be incorrect, but it should be something like that.