简体   繁体   English

世界矩阵中的 XNA/C# 2D 坐标缩放到 3D 查看矩阵?

[英]XNA/C# 2D Coordinate Scaling in World Matrix to 3D View Matrix?

This is my Transform.这是我的变换。 I got it from an example of a simple 2D camera.我从一个简单的 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;
    } 

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.但是,它不会改变 Z 深度。

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.我假设为了做到这一点,我需要改变相机和世界矩阵之间的 Z 距离。

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.我对 XNA 如何在 draw 方法中使用它们的了解更少。 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).到目前为止,我想把我的头发从无结果的搜索中拉出来......我只需要世界坐标来缩放缩放,这样在我的鼠标在预缩放 X.60 之前 Y.60 将在 X .600 Y.600 缩放后(即:缩放级别 0.1)。 But my mouse has not moved, only the world got bigger in view (or shrank).但是我的鼠标没有移动,只是世界变得更大(或缩小)。

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. @RogueDeus 在使用相机放大或缩小时试图转换缩放的输入坐标。 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:因此,如果他的比例矩阵是这样创建的:

Matrix.CreateScale(zoom, zoom, 0);

The mouse coordinates should be inverse scaled and shifted by the necessary translation:鼠标坐标应按必要的平移进行反向缩放和移动:

float ViewportWidth = graphicsDevice.Viewport.Width;
float ViewportHeight = graphicsDevice.Viewport.Height;

Matrix scale = Matrix.CreateScale(zoom, zoom, 0);
Matrix inputScalar = Matrix.Invert(scale);

...

public MouseState transformMouse(MouseState mouse)
{
    /// Shifts the position to 0-relative
    Vector2 newPosition = new Vector2(mouse.X - ViewportWidth,
                                      mouse.Y - ViewportHeight);

    /// Scales the input to a proper size
    newPosition = Vector2.Transform(newPosition, InputScalar);

    return new MouseState((int)newPosition.X, (int)newPosition.Y,
                          mouse.ScrollWheelValue, mouse.LeftButton,
                          mouse.MiddleButton, mouse.RightButton,
                          mouse.XButton1, mouse.XButton2);
}

You are using 2D coordinates, therefore the Z coordinate is of absolutely no importance.您使用的是 2D 坐标,因此 Z 坐标绝对不重要。 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.实际上,您使用的比例矩阵( Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) )将 Z 坐标乘以 0,实际上将其设置为 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?你能试着解释一下吗?

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.因此,由于变换受 Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) 中的缩放影响,mouseState X 和 Y 坐标也需要通过它缩放以虚拟镜像世界矩阵。

    //Offsets any cam location by a zoom scaled window bounds
    Vector2 CamCenterOffset
    {
        get { return new Vector2((game.Window.ClientBounds.Height / Zoom)
             * 0.5f, (game.Window.ClientBounds.Width / Zoom) * 0.5f);
        }
    }

    //Scales the mouse.X and mouse.Y by the same Zoom as everything.
    Vector2 MouseCursorInWorld
    {
        get
        {
            currMouseState = Mouse.GetState();
            return cameraPosition + new Vector2(currMouseState.X / Zoom,
                currMouseState.Y / Zoom);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM