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

Aspect Ratio Anomolies with WinForm Resizing

Last post 07/11/2009 11:27 by BShields. 6 replies.
  • 06/11/2009 5:20

    Aspect Ratio Anomolies with WinForm Resizing

    Is there some way to keep a consistent aspect ratio for your viewport, even as the window resizes? As it is, my supposedly square grid shows up as a rectangle, as do the individual cells. With the end goal of creating a WYSIWYG editor, this does not bode well for business.

    Along with that, I'm not sure my Viewport.Unproject() method is working correctly with these anomalies (I don't know that it isn't working, so don't worry about that). Any suggestions?

    Playing with the viewports in Hammer seems to change the FOV of the camera, so perhaps that has something to do with it?
    Freefall Game Engine - In Development
  • 06/11/2009 6:15 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    The only way to keep a square viewport aspect ratio is to not draw to certain parts of the viewport it if is not square. This would be akin to letterboxing/pillarboxing of movies on TVs.
    The thing most people do, though, is to simply match the projection matrix to the aspect ratio of the viewport. If your viewport is 700 pixels wide and 500 pixels tall, then multiply the Y column of the projection matrix by 500/700 and all is good. Or use a version of the projection matrix creation functions (ortho and perspective) that allow specifying an aspect ratio.
    Viewport.Unproject() works fine, but you should be careful to pass in the right range of Z values, else you'll probably get confusing results back! IIRC, "0" is a good value for Z; this will give you a point on the near clipping plane.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 06/11/2009 14:26 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    Hmm. I've always used Matrix.CreatePerspectiveFieldOfView(), which does allow for an aspect ratio parameter. The way I have my camera class set up, it rebuilds the projection matrix every time a relevant parameter is changed, such as the clip planes, field of view, or the aspect ratio.

    In the default sizing for the XNA game window, the resolution is 800 x 600, right? This would create an aspect ratio of 4/3, or 4:3. Standard definition. The UI of my editor, however, is a menu strip on top, a button strip below that, a panel set to fill the form, with a status bar below that. Pretty minimalist. By default, when the form loads, the panel dimensions are set to 800,600, with the form set a little bigger than that. Even though this matches the aspect ratio (and even the size) of the XNA window, supposedly square gird cells appear rectangular.The issue just gets worse as the form resizes wider than it is tall.

    Just to clarify, I am resetting the graphics device on resizing the form. This could be good or bad. I'm not really sure.
    Freefall Game Engine - In Development
  • 06/11/2009 14:38 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    Answer
    Reply Quote
    Okay, I found the problem. For some reason, the quotient of 1024 and 662 was coming out to be 1 (???). Changing "cam.AspectRatio = mainViewport.Width / mainViewport.Height" to "cam.AspectRatio = (float)mainViewport.Width / mainViewport.Height" fixed the problem.
    Freefall Game Engine - In Development
  • 06/11/2009 16:17 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    Answer
    Reply Quote
    ShadowyCore:
    For some reason


    The reason is that when you divide an integer by another integer, the result is also integer.

    ShadowyCore:
    (float)mainViewport.Width / mainViewport.Height


    Or you could use Viewport.ApsectRatio.
    XNA Framework Developer - blog - homepage
  • 07/11/2009 5:52 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    Thanks Shawn. I should have guessed that the integer division of 1024 and 662 would come to .64, which would then be rounded to 1. That does make sense.
    Freefall Game Engine - In Development
  • 07/11/2009 11:27 In reply to

    Re: Aspect Ratio Anomolies with WinForm Resizing

    ShadowyCore:
    Thanks Shawn. I should have guessed that the integer division of 1024 and 662 would come to .64, which would then be rounded to 1. That does make sense.
    1024 / 662 = 1.5468277945619335347432024169184, which is cast to an integer, which the truncates all the decimals; no rounding done at all.

    The only way you'd get .64 with those numbers is the inverse of the division you're describing (662 / 1024 = 0.646484375); with integer division the result would be zero, because the decimals are truncated, not rounded.
Page 1 of 1 (7 items) Previous Next