I've computed a Viewport that is fair in size (a proper aspect ratio to the given device) and then I calculate my ScaleMatrix like so:

  scaleMatrix = Matrix.CreateScale(
        (float) GraphicsDevice.Viewport.Width/virtualWidth,
        (float) GraphicsDevice.Viewport.Width/virtualWidth,

That by itself, works great. The game scales and everything works fine. However, I'm not trying to add translation to track my character and some things are going a bit wonky.

I'm trying to construct my full transform like so:

   _transform =
                Matrix.CreateTranslation(new Vector3(-pos.X, -pos.Y, 0))*scale
                * Matrix.CreateTranslation(new Vector3(_viewPortWidth * 0.5f, _viewPortHeight * 0.5f, 0));

Am I doing something wrong? The results are not what I expected; the position is the position of the character. Before i added the scaling code, something like this worked for translation.

Here's an image to demonstrate what I want, sorry for the excessive size: =enter image description here

Here's a screenshot of what I currently get:

enter image description here

I'm looking to find a way to achieve the first result with automatic scaling to the maximum viewport size.

  • \$\begingroup\$ In other words, you want a 2D camera that tracks the player without exceeding the boundaries of your level? Perhaps this question helps: gamedev.stackexchange.com/questions/14563/… \$\endgroup\$ Mar 6, 2013 at 0:48
  • \$\begingroup\$ Not quite. I'm having issues with my positions, too quite likely, though. I'm not sure where my issue but I think it's the way I snap position, or the way the transform is calculated. How can I calculate the amount of zoom required so I can properly use this in my position snapping and other transforms? (float) GraphicsDevice.Viewport.Width/virtualWidth, seems okay for the matrix but not anything else. \$\endgroup\$ Mar 6, 2013 at 6:17
  • \$\begingroup\$ You're using width twice for scale in the first code segment... shouldn't one of them be height? \$\endgroup\$
    – Exilyth
    Mar 7, 2013 at 10:45
  • \$\begingroup\$ No, that's correct. :( \$\endgroup\$ Mar 8, 2013 at 2:16

1 Answer 1


There are two parts to the problem :

  1. To compute the camera size you will only need to scale it down from your maximum camera size if the map is smaller than that size. When scaling, you need to select the smallest dimension of your map, vertically or horizontally, and work out the >1 ratio of width:height or height:width. You want to do this because you always want the graphics to fill the scene-view and the larger of the two dimensions to reach off-screen. Working that out and using it to scale your map will assure that your scene-view is always full.

  2. You also want to do a separate bounds check, as Cong Xu mentioned, so your camera follows your character, except the camera's position is bound within a camera safe region. That region will be 1/2 camera width and half camera height in from the edges. Notice that this will need to be recomputed for each map as the camera size will change.

  • \$\begingroup\$ I've done the scaling up above as seen; the problem is the position bounding. I need to multiply by scaling in my position bounding box check I'm nearly positive - but I haven't quite worked out what that scale should be. \$\endgroup\$ Mar 11, 2013 at 21:52

You must log in to answer this question.

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