XNA/C# 世界矩阵中的 2D 坐标缩放到 3D 视图矩阵?
这是我的转变。我从一个简单的 2D 相机的例子中得到了它。
public Matrix Transform(GraphicsDevice graphicsDevice)
{
float ViewportWidth = graphicsDevice.Viewport.Width;
float ViewportHeight = graphicsDevice.Viewport.Height;
matrixTransform =
Matrix.CreateTranslation(new Vector3(-cameraPosition.X, -cameraPosition.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
Matrix.CreateTranslation(
new Vector3(ViewportWidth * 0.5f, ViewportHeight * 0.5f, 0));
return matrixTransform;
}
如果我理解正确的话,它允许滚动(旋转)、缩放时的精灵比例变化以及世界和相机之间的平移,以进行简单的上、下、左、右控制。但是,它不会改变 Z 深度。
但我需要的是游戏世界的缩放,而不仅仅是绘制的精灵。我假设为了做到这一点,我需要改变相机和世界矩阵之间的 Z 距离。
我对编程非常陌生,一般对矩阵只有简单的了解。我对 XNA 如何在绘制方法中使用它们更不了解。到目前为止,我感觉像是在徒劳地寻找答案中拔出头发......我只需要世界坐标来缩放缩放,这样在我的鼠标在预缩放之前 X.60 Y.60 将位于 X .600 Y.600 后缩放(即:缩放级别 0.1)。但我的鼠标没有移动,只是视野变大(或缩小)。
This is my Transform. I got it from an example of a simple 2D camera.
public Matrix Transform(GraphicsDevice graphicsDevice)
{
float ViewportWidth = graphicsDevice.Viewport.Width;
float ViewportHeight = graphicsDevice.Viewport.Height;
matrixTransform =
Matrix.CreateTranslation(new Vector3(-cameraPosition.X, -cameraPosition.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
Matrix.CreateTranslation(
new Vector3(ViewportWidth * 0.5f, ViewportHeight * 0.5f, 0));
return matrixTransform;
}
If I understand it correctly, it allows for a roll(rotation), sprite scale change on zoom, and translation between world and camera for simple up, down, left, right controls. However, it does not alter the Z depth.
But what I need is for the game world to zoom, not just the sprites drawn. And I assume in order to do this I need to change the Z distance between the camera and the world matrix.
I am VERY NEW to programming and have only a simple understanding of matrix in general. I have even less understanding as to how XNA uses them in the draw method. And so far I feel like pulling my hair out from a fruitless search for answers... I just need the world coordinates to scale on zoom, so that before my mouse at a pre-zoom X.60 Y.60 will be at X.600 Y.600 post-zoom (ie: zoom level 0.1). But my mouse has not moved, only the world got bigger in view (or shrank).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这个问题很旧,但这是为了以防万一有人遇到这个问题并且找不到解决方案。 @RogueDeus 在用相机放大或缩小时试图转换缩放的输入坐标。为了缩放鼠标,您所需要的只是获得缩放的逆矩阵。
因此,如果他的缩放矩阵创建如下:
鼠标坐标应逆缩放并移动必要的平移:
I know this question is old, but this is in case anyone comes across this problem and can't find a solution. @RogueDeus was trying to convert scaled input coordinates when he was zooming in or out with his camera. In order to scale the mouse, all you need is to get the inverse matrix of the scale.
So if his scale matrix was created as this:
The mouse coordinates should be inverse scaled and shifted by the necessary translation:
您使用的是 2D 坐标,因此 Z 坐标绝对不重要。事实上,您使用的缩放矩阵 ( Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) )将 Z 坐标乘以 0,有效地将其设置为 0。
因为此缩放矩阵是在视图矩阵中,它将缩放整个世界。我不确定是否真正理解你的问题。您能再解释一下吗?
You are using 2D coordinates, therefore the Z coordinate is of absolutely no importance. In fact, the scale matrix you are using (
Matrix.CreateScale(new Vector3(Zoom, Zoom, 0))
) multiply the Z coordinate by 0, effectively setting it to 0.As this scale matrix is in the view matrix, it will scale the entire world. I am not sure to really understand your problem. Could you try to explain it a litle more, please?
我似乎已经弄清楚如何使坐标缩放......
我假设当前的鼠标状态将反映其单击的世界矩阵,但显然它实际上从未这样做。它始终链接到视图矩阵。 (屏幕本身)并且该值需要与世界矩阵(在变换中)一起缩放。
因此,由于变换是由 Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) 中的 Zoom 影响的,所以 mouseState X & 也受到 Zoom 的影响。 Y 坐标需要通过它进行缩放以虚拟地镜像世界矩阵。
I seem to have figured out how to get the coordinates to scale...
I was assuming that the current mouse status would reflect the world matrix its clicked on, but apparently it never actually does this. It is always linked to the view matrix. (The screen itself) and that value needs to scale along with the world matrix (in the transform).
So as the transform is effected by Zoom in the Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) so too does the mouseState X & Y coordinates need to be scaled by it to virtually mirror the world matrix.