# Automatically zoom out the camera to show all players

I am building a game in XNA that takes place in a rectangular arena. The game is multiplayer and each player may go where they like within the arena. The camera is a perspective camera that looks directly downwards.

The camera should be automatically repositioned based on the game state. Currently, the xy position is a weighted sum of the xy positions of important entities.

I would like the camera's z position to be calculated from the xy coordinates so that it zooms out to the point where all important entities are visible.

My current approach is to:

1. hw = the greatest x distance from the camera to an important entity
2. hh = the greatest y distance from the camera to an important entity
3. Calculate z = max(hw / tan(FoVx), hh / tan(FoVy))

My code seems to almost work as it should, but the resulting z values are always too low by a factor of about 4.

Any ideas?

I would divide FoVx/FoVy by 2.

z = max(hw / tan(FoVx/2), hh / tan(FoVy/2))


• Hmmm I thought that the XNA fov angles were already for half the view cone? – user36159 Oct 17 '13 at 21:35
• @user36159 I'm not sure what made you think that. The perspective matrix constructor mentions no such half-angle. So to use a trigonometric operation, you'll have to divide the angle in half in order to get the necessary right triangle. – Seth Battin Oct 18 '13 at 5:16
• I got that from here: riemers.net/Forum/index.php?var=175&var2=0 – user36159 Oct 18 '13 at 10:28
• I got it to work using: float zw = hw / (float)Math.Tan(camera.FieldOfViewY / 2f); float zh = hh / (float)Math.Tan(camera.FieldOfViewX / 2f); – user36159 Oct 18 '13 at 10:29
• @Seth thanks for the illustration! I was trying to figure out whether XNA was using half FoV but couldn't find any conclusive results on google. – Jimmy Oct 18 '13 at 19:51