2D 游戏中的 XNA 矩阵相机比例问题

发布于 2024-08-10 21:48:54 字数 1250 浏览 2 评论 0原文

我按照教程进行操作 http://www.david -amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ 实现跟随我的玩家精灵并具有放大/缩小功能的相机。

然而,当我放大/缩小时,相机似乎在移动时慢慢地远离精灵,我认为我没有正确设置位置,但我似乎无法弄清楚它需要是什么。

以下是一些片段,如果有帮助,

  if (cam.Follow)
        {
            RectangleF temp = playerBoundingBox;
            cam.Pos = new Vector2(
                (temp.X + temp.Width / 2)*cam.Zoom,
               temp.Y + temp.Height / 2) * cam.Zoom;
        }



    public Matrix get_transformation(GraphicsDevice graphicsDevice)
    {
        _transform =
            // Add Zoom
              Matrix.CreateScale(
               new Vector3((_zoom * _zoom * _zoom),
                                 (_zoom * _zoom * _zoom), 0))
            // Add Camera Rotation
             * Matrix.CreateRotationZ(_rotation)
            // Add Camera Position
             * Matrix.CreateTranslation(
                new Vector3((graphicsDevice.Viewport.Width * 0.5f) - _pos.X,
                                 (graphicsDevice.Viewport.Height * 0.5f) - _pos.Y,
                                  0));
        return _transform;
    }

请提前致谢。

I followed the tutorial on
http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ to achieve a camera that follows my player sprite with zoom in/out functionality.

however, when I zoom in/out the camera seems to either slowly move away from the sprite whilst moving, I don't think I'm setting the position right, but I can't seem to figure out what it needs to be.

Here's some snippets if it helps

  if (cam.Follow)
        {
            RectangleF temp = playerBoundingBox;
            cam.Pos = new Vector2(
                (temp.X + temp.Width / 2)*cam.Zoom,
               temp.Y + temp.Height / 2) * cam.Zoom;
        }



    public Matrix get_transformation(GraphicsDevice graphicsDevice)
    {
        _transform =
            // Add Zoom
              Matrix.CreateScale(
               new Vector3((_zoom * _zoom * _zoom),
                                 (_zoom * _zoom * _zoom), 0))
            // Add Camera Rotation
             * Matrix.CreateRotationZ(_rotation)
            // Add Camera Position
             * Matrix.CreateTranslation(
                new Vector3((graphicsDevice.Viewport.Width * 0.5f) - _pos.X,
                                 (graphicsDevice.Viewport.Height * 0.5f) - _pos.Y,
                                  0));
        return _transform;
    }

thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-08-17 21:48:54

由于某种原因,我通过 http://xnachat.com/ 找到了答案,

Position = Vector2.Zero;
ScreenPosition = new Vector2(GraphicsDevice.ViewPort.Width / 2, GraphicsDevice.ViewPort.Height / 2);
Zoom = Vector2.Zero;
Rotation = 0;
public virtual Matrix ViewTransformationMatrix() 
{ 
    Vector3 matrixRotOrigin = new Vector3(Position, 0); 
    Vector3 matrixScreenPos = new Vector3(ScreenPosition, 0.0f); 

    return Matrix.CreateTranslation(-matrixRotOrigin) * 
        Matrix.CreateScale(Zoom.X, Zoom.Y, 1.0f) * 
        Matrix.CreateRotationZ(Rotation) * 
        Matrix.CreateTranslation(matrixScreenPos); 
} 

这将是正确使用的矩阵我在原始帖子中发布的矩阵是缩放值的立方体

I found the answer through http://xnachat.com/

Position = Vector2.Zero;
ScreenPosition = new Vector2(GraphicsDevice.ViewPort.Width / 2, GraphicsDevice.ViewPort.Height / 2);
Zoom = Vector2.Zero;
Rotation = 0;
public virtual Matrix ViewTransformationMatrix() 
{ 
    Vector3 matrixRotOrigin = new Vector3(Position, 0); 
    Vector3 matrixScreenPos = new Vector3(ScreenPosition, 0.0f); 

    return Matrix.CreateTranslation(-matrixRotOrigin) * 
        Matrix.CreateScale(Zoom.X, Zoom.Y, 1.0f) * 
        Matrix.CreateRotationZ(Rotation) * 
        Matrix.CreateTranslation(matrixScreenPos); 
} 

would have been the correct matrix to use, for some reason the matrix I posted in my original post cubes the zoom value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文