2 Replies - 1843 Views - Last Post: 29 September 2012 - 08:36 AM

#1 derrace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-September 12

2D Camera, limiting bounds and zoom query.

Posted 28 September 2012 - 12:27 AM

Hi all,

I have been following this tutorial (www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/) and I have build a 2D camera, but I am struggling to understand a particular behaviour of the camera when I am applying a zoom.

For the sake of simplicity, I will be using 1x and 2x zooms to describe my query,
my map size is 512x448 and at 1x, I am setting the res to 256x224, and 512x448 at 2x. I want to keep the amount of "objects" I can see consistent. I am using the zoom function instead of changing the scale in the draw method because I don't want to muck around with handling the scaling for the physics of the game.

graphics.PreferredBackBufferWidth = 256;
graphics.PreferredBackBufferHeight = 224;


public void LookAt(Vector2 position)
        {
            Position = position - ScreenCenter;
        }


In my update method, I have called LookAt and passed in the center position of my player object
Is the above code meant to move the camera, only if the the player is not in the center of the screen?

If I used the class without specifying a Limit object
cam = new Camera2d(GraphicsDevice.Viewport, ZoomScale);



Attached Image

From my understanding, David has implemented a Rectangle Limit object which I have used to specify my maximum map area along the lines:

 cam = new Camera2d(GraphicsDevice.Viewport, ZoomScale) { Limits = new Rectangle(0, 0, (int)level.MapSize.X, (int)level.MapSize.Y) };



While I have managed to get it to work for 1x (+ panning)
Attached Image



Now, when I change it to 2X, I need to set the hard min limit for the Rect to (-128,-112) to ensure my camera displays exactly what the 1x displayed, only 2 times bigger.

else if (Zoom == 2.0f) //xxx hardcoded values for testing
                    {
                        pos.X = MathHelper.Clamp(pos.X, -128, Limits.Value.X + Limits.Value.Width - viewport.Width / Zoom);
                        pos.Y = MathHelper.Clamp(pos.Y, -112, Limits.Value.Y + Limits.Value.Height - viewport.Height / Zoom);
                    }



Attached Image

I have obtained the values by visually calculating the offset. My question is, why can't I set the limit's origin to (0,0)? Is the implementation in the tutorial wrong, or am I failing to understand how it works?



The code is as follows:

class Camera2d
    {
        public Vector2 ScreenCenter;
        public float Zoom = 1; //default
        private readonly Viewport viewport;
        private Vector2 pos;
        private Rectangle? limits;
        public Vector2 Position
        {
            get
            {
                return pos;
            }
            set
            {
                pos = value;
                if (Limits != null)
                {
                    if (Zoom == 1.0f)
                    {
                        pos.X = MathHelper.Clamp(pos.X, Limits.Value.X, Limits.Value.X + Limits.Value.Width - viewport.Width);
                        pos.Y = MathHelper.Clamp(pos.Y, Limits.Value.Y, Limits.Value.Y + Limits.Value.Height - viewport.Height);
                    }
                    else if (Zoom == 2.0f) //xxx hardcoded values for testing
                    {
                        pos.X = MathHelper.Clamp(pos.X, -128, Limits.Value.X + Limits.Value.Width - viewport.Width / Zoom);
                        pos.Y = MathHelper.Clamp(pos.Y, -112, Limits.Value.Y + Limits.Value.Height - viewport.Height / Zoom);
                    }
                }
            }
        }

        public Rectangle? Limits
        {
            get
            {
                return limits;
            }
            set
            {
                if (value != null)
                {
                    // Assign limit but make sure it's always bigger than the viewport
                    limits = new Rectangle
                    {
                        X = value.Value.X,
                        Y = value.Value.Y,
                        Width = System.Math.Max(viewport.Width, value.Value.Width),
                        Height = System.Math.Max(viewport.Height, value.Value.Height)
                    };
                    // Validate camera position with new limit
                    Position = Position;
                }
                else
                {
                    limits = null;
                }
            }
        }
        public Camera2d(Viewport viewport, float zoom)
        {
            Zoom = zoom;
            this.viewport = viewport;
            ScreenCenter = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);                    
        }
        public Matrix GetViewMatrix(Vector2 parallax)
        {
            return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) *
                   Matrix.CreateTranslation(new Vector3(-ScreenCenter, 0.0f)) *
                   Matrix.CreateScale(Zoom, Zoom, 1.0f) *
                   Matrix.CreateTranslation(new Vector3(ScreenCenter, 0.0f));
        }
        public void LookAt(Vector2 position)
        {
            Position = position - ScreenCenter;
        }
        public void Move(Vector2 displacement)
        {
             Position += displacement;
        }
    }



Any inputs is much appreciated.

Thank you for your time.

ric

Is This A Good Question/Topic? 0
  • +

Replies To: 2D Camera, limiting bounds and zoom query.

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: 2D Camera, limiting bounds and zoom query.

Posted 28 September 2012 - 09:23 PM

Hello,

Not sure if you saw this on the original tutorial, but I think it might be what you're looking for...

http://www.david-gou...ment-with-zoom/
Was This Post Helpful? 0
  • +
  • -

#3 derrace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-September 12

Re: 2D Camera, limiting bounds and zoom query.

Posted 29 September 2012 - 08:36 AM

View PostLiberLogic969, on 28 September 2012 - 09:23 PM, said:

Hello,

Not sure if you saw this on the original tutorial, but I think it might be what you're looking for...

http://www.david-gou...ment-with-zoom/



Hi, thanks for the reply. Ya I have read that prior, it doesn't support parallax, something I would like to have in my game. I am just trying to understand why my limit's position needs to be offset-ed when more than 1 x zoom. It almost feels like the camera's (0,0) isn't the same (0,0) that is drawn by the spritebatch when zoom is not 1x.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1