It's me again
I have a Camera2D with a DefaultViewportAdapter.
It works flawlessly until I resize the window.
When I resize the window HORIZONTALLY and I call the "LookAt" method on the Camera.BoundingRectangle.Center it will start scrolling UP - the Y coordinate will decrease (the camera will go up).
When I resize the window VERTICALLY and call the "LookAt", it will start scrolling LEFT in a similar fashion.
When I resize both, it will go up-left, diagonally.
I tried everything I could find online. Subscribing to Window_ClientSizeChanged event, setting the PreferredBackBufferWidth/Height manually, calling reset on the GraphicsDevice, creating a new ViewPort rectangle.
I think the Camera.BoundingRectangle coordinates are correct after resizing, but still, somehow the LookAt method misbehaves.
So, for clarity's sake, the problem is:
When I call
Camera.LookAt(Camera.BoundingRectangle.Center); without resizing it works correctly (i.e. it does NOT move the camera).
When I call it AFTER resizing, it does move the camera.
This is how the method looks internally:
public void LookAt(Vector2 position)
Position = position - new Vector2(_viewportAdapter.VirtualWidth/2f, _viewportAdapter.VirtualHeight/2f);
The behaviour before resize:
So let's say I have a window 1280x720.
_viewportAdapter.VirtualWidth/2f will be 640,
_viewportAdapter.VirtualHeight/2f will be 360
The default, starting Position of the Camera is (0, 0). The Position indicates the position from the Origin, which is, by default is
Origin = new Vector2(viewportAdapter.VirtualWidth/2f, viewportAdapter.VirtualHeight/2f);.
It is the measured from the center of the camera, not the top-left corner (I think).
So, at (0, 0), the Camera.BoundingRectangle.Center will be (640, 360). (and at (-640, -360) the Center will be (0, 0))
When I call the LookAt with this, the calculated Position will be (0, 0), so it remains unchanged, and it will work with other values too, not just with (0, 0). It works as expected.
The behaviour after resize
Let's say I resized the window to 1280x730.
_viewportAdapter.VirtualWidth/2f will be unchanged, 640,
_viewportAdapter.VirtualHeight/2f will be 365
Position will remain unchanged (0, 0).
The Camera.BoundingRectangle.Center will get changed to (640, 361,6666).
Position = (640, 361,6666) - new Vector2(640, 365) = (0, -3,4444);
The Camera will move upwards.