How to keep Two object always inside the camera view frustum

Hi, all. In one of my scene, I have a Sprite object and a Line (created from multiple points). Like a ball dropping on the ground. Sometimes the ball goes too high from ground sometimes not that high. I know the vertical distance of the sprite object and the line. I want to dynamically update the camera in such a way that, i can always see the ball and the line in the camra view frustrum. The maximum distance of the object and the line can be 10unit.

My object size is 1-2unit at max. My camera position is,

CameraPosition = ObjectPosition + (HorozontalDirection * HorizontalOffset + VerticalDirection * VerticalOffset) * DollyInOutValue

My camera FOV = 30

I see that for this setting, with a horizontal offset 3.5m and verticalOffset -0.5m, if i place the camera, I can see the object and line (untill their distance is 0.8m or less). So if I linearly change the DollyValue based on the distance of Object and Line, when the distance is 2-3m, i have to zoom out too much that my object becomes very tiny.

I want to know how the scaling factors for the objects work in terms of camera distance, fov etc. How i keep the objects size as it is (may be orthographic camera) but zoom in/out smoothly based on the object and line vertical distance to keep both of them always inside camera frustrum.

frustum… one R.
https://threejs.org/docs/index.html#api/en/math/Vector3.project

https://threejs.org/docs/index.html#api/en/math/Vector3.unproject

You can maybe use project/unproject to go from NDC camera space ( -1 to 1 on each axis… ) for each point… find out how you have to move the NDC box to fit the 2 points, and then unproject that resulting offset back into world space a move the camera. Maybe :slight_smile:

No, I can find the mid point of the two object, (which is my cameraLookAt as well) and also from I know the half length (combining two objects) so that i set my camera at such a position that the vertical frustum of the camera always fit the object in the scene (div/canvas) that i have.

For this I use simply,
horizontal_distance = ((1/tan(fov/2)) * (half_my_vertical_length) )* some_offset
My canvas ratio(width/height) is 1.68.

However though in most cases it works, my objets are fit in the camera, but sometimes it doesnt fit. Why is that? Isn’t it mathmetically should fit always?

N.B. My camera position, lookat point is fine. But I think something wrong with this horizontal distance, am i missing something? I convert camera FOV to radian btw.

I’ve gone down this rabbit hole before and there are some issues. With an orthographic camera it’s relatively easy. you get the bounds of both objects, accumulate them and then center the camera.

With a perspective camera its much harder. Consider the fact that when an object gets close the edge of the display in a perspective camera, the visual shape of the object changes…

So any change in positioning of the camera, will cause the object to move, and then it’s bounding box will be different, and your guess about its visual bounds will change… It’s a tricky problem.