Move camera back to fully see an object on viewport

Hello.

I would like to make the camera move so that a 3D object in the world is fully visible in the viewport. For example, a zoom out effect to fully see a rectangular shaped level.
I obviously manage to calculate the size (width and length) of the object, the maximum size it must have on the screen to fit to the length and/or width of the viewport. But I can’t find their relationship to calculate how much the camera must move back to switch from the first to the other.

I found several ideas but they are not correct as they require loops (e.g. moving the camera back until the corners of the level are rendered).

Thank you in advance for your help.

Hey there @Mathieu-FR! Welcome back to the community! This is a bit thought provoking! My first thought went to possibly getting the bounding box and checking the extent using Get Actor Bounds.

Hey! Thanks for your answer.

That is actually one of the solutions that I mention at the end of my post: I know the size of the object and I know the size of the viewport so I can move the camera back as long as it extends beyond the frame.
But this doesn’t seem like a correct method to me since it uses a loop. I don’t think this is the method generally used in games when there is some sort of “zoom out focus” on an object…

In my opinion, if you want to guarantee it works on all objects you use it on, that’d probably be the best method, and bounding boxes are just cubes so they have few vertices to cover if you had to. Alternatively if all of your objects are of a similar size/shape, you could get away with using the bounds size normalized as a fuzzy “zoom” variable but that’d be more error prone on large or oblong objects. Are you going to be doing this rapidly on tons of objects at once or just one at a time like a map editor scenario?

To answer your question: my current project is a 2D platform game (side view). The idea is to zoom out so that the player can view the entire current level. The levels are rectangular in shape so I can easily calculate their size.

I may have found a clue: I have the impression that we must use trigonometry. I manage to get the desired result but if the size of the viewport changes, it no longer works. I imagine that you have to manage the vertical FOV and the horizontal FOV as parameters in the formulas but I don’t see how. I found some advice in the forum but apparently there has been a change in FOV management since version 5.2 so the formulas I found no longer work…