XNA 2D凸轮变焦不正确

本文关键字:变焦 不正确 2D XNA | 更新日期: 2023-09-27 18:18:06

我基于http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/中的类构建了一个相机。我目前正在制作一款类似太空射击游戏(但带有故事等内容),并希望首先确保镜头能够正常工作。但极速没按我想的那样工作。我不知道为什么,但我希望你能帮助我^^问题是,精灵在某个方向移动了大约30个像素。可能是我没有正确设置主飞船的位置,或者是相机出了问题。游戏全屏运行

标志性图示方法:

InputHandler.Update(gameTime, graphics.GraphicsDevice);
        World.Camera.Position = new Vector2(Player.Ship.Position.X + Player.Ship.Texture.Width/2, Player.Ship.Position.Y+Player.Ship.Texture.Height/2);
        World.Camera.Zoom = InputHandler.ZoomValue;

我将主飞船纹理的一半大小添加到相机位置,这样纹理的中间应该在中间。没有它也不能工作…

如何设置全屏(我知道…这不是我应该做的方式,但它不能与正常代码一起工作。)

graphics.PreferredBackBufferHeight = 10000;
        graphics.PreferredBackBufferWidth = 10000;
        graphics.ToggleFullScreen();
        graphics.PreferMultiSampling = true;
        graphics.IsFullScreen = true;
        graphics.ApplyChanges(); 
InputHandler类:
 public static void Update(GameTime gameTime, GraphicsDevice graphics)
    {
        _oldKeyboardState = keyboardState;
        _oldMouseState = mouseState;
        keyboardState = Keyboard.GetState();
        mouseState = Mouse.GetState();
        MousePosition = new Vector2(mouseState.X, mouseState.Y);
        MatrixMousePosition = Vector2.Transform(MousePosition, Matrix.Invert(World.Camera.GetTransformation(graphics)));
        OldScrollWheelValue = ScrollWheelValue;
        ScrollWheelValue = mouseState.ScrollWheelValue;
        if (ScrollWheelValue > OldScrollWheelValue) ZoomValue += 0.1f;
        if (ScrollWheelValue < OldScrollWheelValue) ZoomValue -= 0.1f;
        ZoomValue = MathHelper.Clamp(ZoomValue, 0.5f, 1.5f);
    }

最后是camera类

protected float _zoom; // Camera Zoom
    public Matrix _transform; // Matrix Transform
    public Vector2 _pos; // Camera Position
    protected float _rotation; // Camera Rotation
    public float Zoom
    {
        get { return _zoom; }
        set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } 
    }
    public float Rotation
    {
        get { return _rotation; }
        set { _rotation = value; }
    }
    public Vector2 Position
    {
        get { return _pos; }
        set { _pos = value; }
    }
    public Camera()
    {
        _zoom = 1.0f;
        _rotation = 0.0f;
        _pos = Vector2.Zero;
    }
    public Matrix GetTransformation(GraphicsDevice graphicsDevice)
    {
        var viewport = graphicsDevice.Viewport;
        _transform =
          Matrix.CreateTranslation(new Vector3(-_pos.X*Zoom, -_pos.Y*Zoom, 0)) *
                                     Matrix.CreateRotationZ(Rotation) *
                                     Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                     Matrix.CreateTranslation(new Vector3(viewport.Width * 0.5f, viewport.Height * 0.5f, 0));
        return _transform;
    }

XNA 2D凸轮变焦不正确

我知道你做错了什么,这一行:

transform = Matrix.CreateTranslation(new Vector3(-_pos.X*Zoom, -_pos.Y*Zoom, 0)) * ....
应:

transform = Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) * ....

因为现在当你缩放的时候你也会平移图像,这会导致移动的精灵