Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What is the most practical way to implement camera/viewport to a 2D-game?

I've read, that I should store the object world position instead of position relative to the screen?

Current situation:

I have implemented a simple 2D-game where I load objects and levels from XML-files. Currently the level XML-file looks like this:

   <tile obj="ground" x="0" y="555" />
   <tile obj="ground" x="16" y="555" />
   <tile obj="ground" x="32" y="555" />

All objects have a 2d-vector "position" storing their current location on the screen.

What I want it to be:

Viewport/gameworld illustration

In the picture:

  • Camera is either 800x600 or 640x480
  • Blocks and sprites are 16x16 pixels.
  • World size may vary
  • The coordinates probably should be normalized relative to the world, not to the screen?
  • Viewport position relative to player's x, y and moves when the player reaches camera dead zone (similar to this video).

I'm asking pseudo examples / articles, but if you need to know what I'm using for the development: SDL & C/C++.

share|improve this question
Add your third link in the comments here and I can add it to your question. – Byte56 Dec 25 '12 at 16:38
Here's what I meant with the camera dead zone: – bluekirai Dec 25 '12 at 16:47
possible duplicate of… – zehelvion Dec 25 '12 at 18:34
Hello @Arthur Wulf White, care to elaborate? Thanks. – bluekirai Dec 25 '12 at 18:47
The camera you mention is a specific version of a general 2d camera that is only used to offset the view (no rotation and zooming). The tracking behavior can be implemented by checking the distance between the player character and the camera, moving the camera if the distance is too great. – zehelvion Dec 25 '12 at 19:35

2 Answers 2

up vote 13 down vote accepted

You need to have every object positioned relative to the world instead of the screen. Your camera should also have its own world coordinates so it can be drawn at a relative position in the world. It may also be convenient to have your camera follow an object, so wherever the object is, the camera just uses its coordinates. Typically the camera's coordinates will position it from the upper left corner. This means the camera would have a world position of approximately (0,24) in the picture.

As for actually drawing the objects the camera can "see", you must draw all objects relative to the camera's world coordinates. To compute an object's screen position relative to the camera, simply do:

int screenX, screenY; //screen position of the object being drawn

screenX = object.x-camera.x;
screenY = object.y-camera.y;

Obviously some objects are not actually visible to the camera, so you may want to implement a view culling system.

share|improve this answer

It's best to do all of this in the GPU using the World and View matrices, not by modifying where you draw the objects on the CPU.

That way, you can change the camera arbitrarily (even zoom it in and out!) and it will just magically work. You can still do view culling as well to save on draw time. And none of your code for drawing the world will have to change after you've setup the view and world matrices correctly.

In SDL, you can probably just inline OpenGL calls such as glOrtho and glTranslate.

See this thread.

share|improve this answer
Can someone explain the downvote? This makes sense. – Hello World Mar 8 at 20:53
I did not down-vote, but I think it's because this doesn't even answer the question. The question is about how to calculate something, not whether it is more efficient or easier to do on a GPU vs CPU. The OP even said he's looking for pseudo examples. There's no doubt that using a combination of camera/world/model matrices would be more efficient, so mklingen has a point a least. – Dan Watkins Apr 16 at 1:27
This answer is not bad at all! Its more specific to OpenGL/DX development, but its the right approach as you can just compute a translation matrix based on camera coors and move objects via cam matrix, without changing their actual positions. – nenchev Sep 25 at 0:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.