Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 2 down vote accepted

I would divide FoVx/FoVy by 2.

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

enter image description here

share|improve this answer
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: – 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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.