tags:

views:

478

answers:

1

Hi, 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.

A: 

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

Shahin