• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
derrace

2D Camera, limiting bounds and zoom

2 posts in this topic

Hi all,

I have been following this tutorial ([url="http://www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/"]http://www.david-gou...rolling-in-xna/[/url]) 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.

[code]
graphics.PreferredBackBufferWidth = 256;
graphics.PreferredBackBufferHeight = 224;[/code]

[code]
public void LookAt(Vector2 position)
{
Position = position - ScreenCenter;
}
[/code]
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
[code]
cam = new Camera2d(GraphicsDevice.Viewport, ZoomScale);
[/code]

[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:

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

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.

[code]
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);
}
[/code]

[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:

[code]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;
}
}
[/code]

Any inputs is much appreciated.

Thank you for your time.

ric
0

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0