# How can I create a 2D camera with upscaling and translation?

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,
1f);


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: =

Here's a screenshot of what I currently get:

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

• 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/… – congusbongus Mar 6 '13 at 0:48
• 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. – Vaughan Hilts Mar 6 '13 at 6:17
• You're using width twice for scale in the first code segment... shouldn't one of them be height? – Exilyth Mar 7 '13 at 10:45
• No, that's correct. :( – Vaughan Hilts Mar 8 '13 at 2:16