I'm trying to place my game camera so that the resulting view for the player will encompass a selection of points in world space. I have calculated the central averaged position of the points, worked out the unit vector in which I want the camera to travel away from the central point, but I can't seem to quite get my head around the required distance to move the camera.
I can't figure out how to take the FOV of the camera, the list of points, and the facing angle/projection matrix of the camera to determine how far back I need to pull the camera. I'm currently thinking that I might be able to reverse the 'is point on screen' equation using the cameras projection matrix, then get a required distance for each point and use the biggest distance. I'm just not sure how to reverse that equation - or if I'm even on the right track here.
Can someone point me in the right direction please?
I would do it the following way (using OpenGL matrix):
Place the camera at the median point of your point cloud. Transform all points to camera (view, eye) space.
Now translating camera along it's local Z is equivalent to translating all points along global Z in camera space. I.e. you have to find for each point, which is the smallest translation along z that moves it inside view frustum.
Let your point (in camera coordinate system) be P(x,y,z)
And after perspective division
Sx = Qx/Qw
Sy = Qy/Qw
Sz = Qz/Qw
You want to ensure that:
-1 <= Sx <= 1
-1 <= Sy <= 1
-1 <= Sz (you do not want to check far plane here, because otherwise your inequalities may become unsolvable)
Now for each point and for each coordinate x, y and z find maximum z value Pz' that fills the conditions of these inequalities.
Required distance to move camera d = Pz - Pz'
While iterating over all points and all coordinates find the maximum d.