0
votes

I built a camera based on the class from http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ . I'm currently making a kinda space-shooter game (but with story and this stuff) and want to have the camera working fine first. But the Zoom doesn't work as I want. I don't really know why, but I hope you can help me ^^ The Issue is, that the sprite moves around for like 30 pixels in some direction. Propbably i didn't set the Position of the main-spaceship properly or the camera is just bugging. The game is running in full-screen.

Game1 Method:

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;

I add half the size of the texture of the main-spaceship to the cameraposition so that the middle of the texture should be in the middle. It doesn't work either without it..

How I set fullscreen (I know... It's not the way I should have done it but it doesn't work with the normal code.)

graphics.PreferredBackBufferHeight = 10000;
        graphics.PreferredBackBufferWidth = 10000;
        graphics.ToggleFullScreen();
        graphics.PreferMultiSampling = true;
        graphics.IsFullScreen = true;
        graphics.ApplyChanges(); 

The InputHandler Class:

 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);
    }

And finally the camera class:

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;
    }
1

1 Answers

1
votes

I see what you did wrong, this line:

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

should be:

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

Because right now when you zoom you also translate the image, which is causing the moving sprites.