我一直在尝试将 David Amador 的这两个样本结合起来:
http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/
http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/
一切正常,除了我在获取鼠标坐标时遇到了一些困难。我能够为每个单独的样本获得它们,但我将两者都考虑在内的数学似乎是错误的。
如果我的虚拟分辨率和正常分辨率相同,则鼠标坐标是正确的。当我在移动相机时坐标慢慢不同步时,我会执行 Resolution.SetVirtualResolution(1920, 1080) 和 Resolution.SetResolution(1280, 720, false) 之类的操作。
这是代码:
public static Vector2 MousePositionCamera(Camera camera)
{
float MouseWorldX = (Mouse.GetState().X - Resolution.VirtualWidth * 0.5f + (camera.position.X) * (float)Math.Pow(camera.Zoom, 1)) /
(float)Math.Pow(camera.Zoom, 1);
float MouseWorldY = ((Mouse.GetState().Y - Resolution.VirtualHeight * 0.5f + (camera.position.Y) * (float)Math.Pow(camera.Zoom, 1))) /
(float)Math.Pow(camera.Zoom, 1);
Vector2 mousePosition = new Vector2(MouseWorldX, MouseWorldY);
Vector2 virtualViewport = new Vector2(Resolution.VirtualViewportX, Resolution.VirtualViewportY);
mousePosition = Vector2.Transform(mousePosition - virtualViewport, Matrix.Invert(Resolution.getTransformationMatrix()));
return mousePosition;
}
在决议中我添加了这个:
virtualViewportX = (_Device.PreferredBackBufferWidth / 2) - (width / 2);
virtualViewportY = (_Device.PreferredBackBufferHeight / 2) - (height / 2);
其他一切都与示例代码相同。提前致谢!