1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I would divide FoVx/FoVy by 2.

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

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ Hmmm I thought that the XNA fov angles were already for half the view cone? \$\endgroup\$
    – user36159
    Oct 17, 2013 at 21:35
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Oct 18, 2013 at 5:16
  • \$\begingroup\$ I got that from here: riemers.net/Forum/index.php?var=175&var2=0 \$\endgroup\$
    – user36159
    Oct 18, 2013 at 10:28
  • \$\begingroup\$ I got it to work using: float zw = hw / (float)Math.Tan(camera.FieldOfViewY / 2f); float zh = hh / (float)Math.Tan(camera.FieldOfViewX / 2f); \$\endgroup\$
    – user36159
    Oct 18, 2013 at 10:29
  • \$\begingroup\$ @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. \$\endgroup\$
    – Jimmy
    Oct 18, 2013 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .