Positioning camera view to focus on a geometry

Hey guys,



I’ve got a problem with positioning my camera, based on a the size of a geomtry.



This is my setup:


  • I've got a camera orbiting around a central node, always looking at the central node

  • I've got a geometry, placed in between the central node and the camera.



How can I calculate the distance to the central node, so that the geometry takes up the most space possible of my camera's viewport?

I've been tinkering about translating my cameras position closer to and further away from the central node, using the Camera's: Camera.FrustumIntersect contains( BoundingVolume bound ); to check if it's contained or not... it just doesn't seem too efficient doing it that way.

So again, how can I calculate the distance to my central node?

Thanks alot.
/Anders

I am just guessing, but I noticed in the examples if you press C you get the position of the camera. So there must be a cam.getPosition() one would think. Likewise with the node, so get both positions and calculate the distance ?

Well, you’re right about the fact that you can get the distance between the camera and the geometry - however.



Imagine that I simply move the camera towards the geometry, and place it right up against the geometry… It will end up with the cameras viewport being covered completely by the geometry, what I want to do is display the entire geometry (having re-read my initial post, I’ve noticed I didn’t make that clear - sorry 'bout that).



So to sum it up.



How far should I move the camera towards (or away from) the center node, to have the viewport display the entire geometry?

What you would want to do first is find the distance between the two farthest apart vertices.



Now you can move your camera back, continuously doing the screen->world coordinate conversion for the 4 edges of your display. The further you move back, the wider the view, the greater the distance between these points… Eventually you will be reach the distance you found in the two outer limits of the geometry and will likely want to stop moving back.



Hope this helps…