XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

Last post 05-07-2008 8:45 PM by Luke DeStevens. 6 replies.
  • 05-06-2008 8:16 PM

    Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

    Hey everyone,

    I've been working on an algorithm to figure out where to position the camera such that everything in a zone of space [defined by Bounds, a set of min and max X, Y and Z coordinates] is visible.  I thought my first attempt was successful, but noticed that it was broken today.  Basically, I figured if I wanted to see the entire X-Z face, I could set the camera's position at a distance from that face equal to the height of a triangle, where

    height = (1/2 the greater of the two dimensions' widths) / Math.Atan(cameraFieldOfView)

    where cameraFieldOfView is the value I pass into Matrix.CreatePerspectiveFieldOfView.  I thought it was a little off because the camera was a little too far away, but then I realized that I forgot to halve the width, and after fixing that, the camera is always too close.

    I completely ingore aspect ratio and presume it plays in somewhere, but I'm not quite sure where.

    I suspect it has something to do with what I think fieldOfView refers to and what it actually is, but I can't find any documentation on precisely what angle that refers to.  I think it's the angle between the center of the screen and the side, meaning a fieldOfView greater than 90 degrees (or Math.PI / 2f radians) would have you seeing stuff behind you.

    Yes, I always convert to radians.  Actually, I usually work directly in radians.

    I've dug around and only found one or two places that even differentiate between horizontal and vertical fields of view.  I presume that the parameter in Matrix.CreatePerspectiveFieldOfView refers to horizontal FOV.  I figured vertical FOV was scaled down using aspect ratio (assuming a width greater than height).

    I've been playing around with my program as well as reading various articles, but I haven't been able to figure it out.  My math seems right to me, but something must be wrong or my algorithm would be working fine.

    Can anyone help me with this problem?  Thanks in advance.
  • 05-06-2008 8:18 PM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

    Here is my code.

    public override void LookAtBottom()
    {
      Bounds b = Scene.GetVisibleBounds();
      Vector3 position = b.Center;
      float width = b.MaxX - b.MinX;
      float w2 = b.MaxZ - b.MinZ;
      if (w2 > width)
        width = w2;
        
      float height = GetTriangleHeight(width / 2f, cameraFov);
      
      if (height < nearPlaneDistance)
        height = nearPlaneDistance * 1.1f;
        
      height += (b.MaxY - b.MinY) / 2f;
      
      position.Y -= height;
      Vector3 faceDirection = new Vector3(0, 1f, 0);
      
      FaceDirectionFrom(faceDirection, position);
    }

    public static float GetTriangleHeight(float baseLength, float topAngle)
    {
      return baseLength / (float)Math.Tan(topAngle);
    }

  • 05-06-2008 8:58 PM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView &quot;fieldOfView&quot; parameter

    Answer
    I played around with it some more, experimenting with changing cameraFov, rotating the camera, and such.  It seems to me that fieldOfView is the full angle, not the half angle, and that the matrix always uses this value as the vertical field of view angle.  That is, fieldOfView could be calculated as the angle formed between the middle bottom of the view frustrum, the camera position, and the middle top of the view frustrum.

    It seems to adjust the horizontal field of view according to the aspect ratio, keeping the vertical field of view constant.  That is, if my viewport is wider than tall, then my horizontal field of view angle will be greater than what I specified in the CreatePerspectiveFieldOfView call, and if my viewport is taller than it is wide, then my horizontal field of view will be less than what I specified in the CreatePerspectiveFieldOfView call.  I suppose this makes sense considering that XNA is designed for games and split screen preserves height first.

    Does this sound accurate to anyone here?

    Thanks for listening to me talk to myself again.  Input would still be appreciated.
  • 05-07-2008 11:14 AM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView &quot;fieldOfView&quot; parameter

    Answer
    well, reflector does seem to offer corroboration that it is keyed off the vertical.
    row two of the matrix is associated with the vertical (up vector) and m22 is derived from the fov.
    row one of the matrix is associated with the horizontal and is derived from the vertical / aspect ratio.

    I think you nailed it.

    Steve H
  • 05-07-2008 11:20 AM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

    Steve, I deleted the code out of your post because, according to the EULA, you cannot use Reflector to read out code and then post it up online. The OP is free to take your advice to use Reflector to check things out, but you shouldn't post up code from the framework that you got using Reflector.
  • 05-07-2008 12:02 PM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

    Sorry, didn't know. I'll keep that in mind.

    Steve H
  • 05-07-2008 8:45 PM In reply to

    Re: Objects in view and Matrix.CreatePerspectiveFieldOfView "fieldOfView" parameter

    Steve,

    Thanks for that confirmation.  God knows I wouldn't have been able to read the vector and matrix math.  I'd have replied sooner, but FireFox has been acting weird all day with the Windows Live thing, but now (unlike previous tries) IE is working fine with this site.  Guess it's a matter of the lunar phases which browser works...

    Strange how things happen at the same time.  I'd never heard of Reflector until a coworker of mine showed it to me earlier today to explore the difference between Quaternion.Concatenate and Quaternion.Multiply, and now you bring it up.  I'd have thought that a decompiler itself would be against the EULA, but I guess not.  Too bad they don't make them for C++...

    Hey Nick, what happens if the fieldOfView is set to just under 180 degrees and the aspect ratio is greater than one?  Would that set the horizontal fieldOfView to be greater than 180 degrees?

    Anyhoo, thanks again for the help.

Page 1 of 1 (7 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG