Fitting an object in camera's view

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Fitting an object in camera's view

      Hi,

      I've been searching through this board about a camera behavior i want to implement. I know this has been asked, and answered by Mr. Mike, a year or two ago but i can't find it. If anyone could find this old topic, i'd really appreciate it.

      I'm thinking of a simple camera behavior where the camera would smoothly zoom in to an object but just enough that the entire 3d model would perfectly fit in the camera's view. More specifically, i'm creating a 3D textured quad with GUI elements rendered in the texture then the camera would zoom in to that quad until it would look like as if it was rendering quads in an orthographic projection; but it would still be in perspective projection. Similar to how does Windows Aera View transitions back.

      [IMG:http://hackspc.com/wp-content/uploads/2010/11/Windows_7_Aero.jpg]

      I'm drawing blank on where to start on the math for this behavior. Can anyone be so kind to explain the mathematics for this? Pseudo-code would even be much helpful. Thanks!

      The post was edited 1 time, last by BrentChua ().

    • I've kinda solved my problem, i think. Thinking deeper my main problem was figuring out the right distance for a given object with varying size to fit in the camera view frustum.

      Doing some basic trigonometry math with some given that we have the camera's Field-Of-View (FOV), size of the object (ObjSize), and we're looking for the distance (Distance) it makes sense to:

      Since: Tan(FOV/2) = opp/adj
      Given: opp = ObjSize/2
      Given: adj = Distance
      Therefore: Tan(FOV/2) = (ObjSize/2) / Distance
      Final: Distance = (ObjSize/2) / tan(FOV/2)

      Haven't tested this out yet and not exactly sure if this is right even. If anymore knows, let me know.