Skip to main content
added 112 characters in body
Source Link
OriginalDaemon
  • 3.5k
  • 19
  • 20

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.

Although I have foundMy understanding is that this code moves the solution,camera such that the answerobject I want to focus on, which is still not complete; whyat (Position.X, Position.Y), is it that this _View matrix willin 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 while the one create, but scaling is always in relation to the other code snippet will zoom in onorigin which would therefore move the top left cornerpoint further away from 0,0 and thus it isn't at the center any more.

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.

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.

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.

Source Link
OriginalDaemon
  • 3.5k
  • 19
  • 20

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.

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.