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 = Matrix.CreateTranslation( 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.
I have found the solution:
_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. Although I have found the solution, the answer is still not complete; why is it that this _View matrix will zoom to the center of the camera while the one create in the other code snippet will zoom in on the top left corner.