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++.

  • 1
    \$\begingroup\$ Add your third link in the comments here and I can add it to your question. \$\endgroup\$
    – House
    Dec 25, 2012 at 16:38
  • \$\begingroup\$ Here's what I meant with the camera dead zone: youtube.com/watch?v=89TRXUm8jMI \$\endgroup\$
    – bluekirai
    Dec 25, 2012 at 16:47
  • \$\begingroup\$ possible duplicate of gamedev.stackexchange.com/questions/38672/… \$\endgroup\$
    – AturSams
    Dec 25, 2012 at 18:34
  • \$\begingroup\$ Hello @Arthur Wulf White, care to elaborate? Thanks. \$\endgroup\$
    – bluekirai
    Dec 25, 2012 at 18:47
  • \$\begingroup\$ 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. \$\endgroup\$
    – AturSams
    Dec 25, 2012 at 19:35

2 Answers 2


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.


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.

  • \$\begingroup\$ Can someone explain the downvote? This makes sense. \$\endgroup\$ Mar 8, 2015 at 20:53
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Apr 16, 2015 at 1:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – nenchev
    Sep 25, 2015 at 0:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .