In XNA I am using a spritebatch to render a board. I have created a camera class which can provide me a view and projection matrix. Currently I am supplying the view matrix to my sprite batch when I call begin.

I can get the view matrix to correctly move everything around and do some zooming in and out. My problem is that I want to zoom in on a specific point (for now the center of the screen) but currently zooming just zooms in on the coordinate 0,0.

Here is how I am constructing the view matrix;

        _ViewMatrix =
                new Vector3(
                    (-_Position.X + _Width * _Zoom / 2),
                    (-_Position.Y + _Height * _Zoom / 2),
                    0) ) *
            Matrix.CreateScale(new Vector3(_Zoom, _Zoom, 1));

How could this be altered to make it scale correctly relative to the given point. Later I will probably want to zoom to the mouse position, but if someone has a simple example I'm sure I can work out how to adapt it.

  • \$\begingroup\$ Not sure I understand your question exactly, but shouldn't you first scale and then translate? Why do you have '_Width * _Zoom / 2' in your code? \$\endgroup\$ May 27, 2012 at 3:46
  • \$\begingroup\$ I tried putting the scaling first, but got the same result. The '_Width * _Zoom / 2' part is to position the camera so that the center of the camera is over the point 0,0. \$\endgroup\$ May 27, 2012 at 13:21
  • \$\begingroup\$ Please, as you found the solution, post it as an answer to your own question. \$\endgroup\$ May 27, 2012 at 18:26

1 Answer 1


The following code creates the desired result.

        _ViewMatrix =
            Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
            Matrix.CreateScale(_Zoom, _Zoom, 1.0f) *
            Matrix.CreateTranslation(_Width / 2, _Height / 2, 0.0f); 

This will cause the camera to zoom into the center of the screen instead of the top left corner.

My understanding is that this code moves the camera such that the object I want to focus on, which is at (Position.X, Position.Y), is in the top left corner (0,0), then I'm scaling by the zoom factor and then finally moving the camera by half width/height so the top left corner is now in the center.

The code in the question would move the object to the center of the camera, but scaling is always in relation to the origin which would therefore move the point further away from 0,0 and thus it isn't at the center any more.


You must log in to answer this question.

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