How To: Position the Camera to View All Objects in a Scene
The Complete Sample
The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample..
Positioning the Camera to View All Objects in a Scene
To position the camera to view all objects in a scene

Create a BoundingSphere class that contains all of the objects in the scene. To create the sphere, loop through all of the objects in the scene, merging the BoundingSphere classes that contain them with BoundingSphere.CreateMerged.

If you are not already tracking the BoundingSphere classes for collision detection, use CreateFromBoundingBox or CreateFromPoints to create them from BoundingBox classes or points.
In this example, the BoundingSphere classes are created from BoundingBox classes.
BoundingSphere GetSceneSphere() { BoundingSphere sceneSphere = new BoundingSphere(new Vector3(.5f, 1, .5f), 1.5f); for (int z = 0; z < 5; z++) { for (int x = 0; x < 5; x++) { BoundingSphere boundingSphere = sphere.Meshes[0].BoundingSphere; boundingSphere.Center = new Vector3(x * 3, 0, z * 3); sceneSphere = BoundingSphere.CreateMerged(sceneSphere, boundingSphere); } } return sceneSphere; }

Set the position of the camera to the center of the BoundingSphere that contains the scene.
cameraPosition = sceneSphere.Center;

Determine the distance from the center of the BoundingSphere that the camera needs to be to view the entire scene.
This distance is equal to the hypotenuse of the triangle formed by the center of the sphere, the desired camera position, and the point where the sphere touches the view frustum. One angle of the triangle is known to be the field of view of the camera divided by two. One leg of the triangle is known to be the radius of the sphere. Given these two measurements, you can calculate the hypotenuse as the radius of the sphere divided by the sine of half the field of view.
float distanceToCenter = sceneSphere.Radius / (float)Math.Sin(FOV / 2);

Get the Backward vector of the view Matrix and flip its X component.
Vector3 back = view.Backward; back.X = back.X; //flip x's sign

To move the camera backward with respect to its orientation, multiply the desired distance by the adjusted back vector from the previous step.
The camera is now facing the center of the sphere containing the scene and is far enough back that the sphere fits in the camera's view frustum.
cameraPosition += (back * distanceToCenter);