2D Camera, limiting bounds and zoom

Started by
1 comment, last by Programming020195BRook 11 years, 6 months ago
Hi all,

I have been following this tutorial (http://www.david-gou...rolling-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);


[attachment=11488:as1.png]

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)
[attachment=11489:as2.png]



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


[attachment=11490:as3.png]

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
Advertisement
200+ views, can someone assist?
I'm going to answer this question in more of a general way, as I don't use XNA anymore.

When you think of a camera, think of the area which is viewable to the player. If your screen is 500 x 500, this is your camera view unless of course you have a GUI taking up the bottom half, ect... If you're looking at adding zoom this is much easier to accomplish by drawing your map to a plane (this would be done by drawing the map as a texture on the object), then changing the Z value of your plane to zoom in or out if you're displaying the level in a 2D fashion. Now your plane could be 1000 in width and 500 in height. When you move your main sprite you also draw him on the plane, and by moving your main sprite your logic function should update his X, Y, values based on which direction you move. On top of this, you will move the plane's X value to simulate the camera moving. When an action scene comes into play, or whatever you need to do to zoom a level, you can just set the Z value to the appropriate number.

If you need more help, do not hesitate to ask!
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

This topic is closed to new replies.

Advertisement