Take the 2-minute tour ×
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:

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

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
1  
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: youtube.com/watch?v=89TRXUm8jMI –  bluekirai Dec 25 '12 at 16:47
    
possible duplicate of gamedev.stackexchange.com/questions/38672/… –  Arthur Wulf White 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. –  Arthur Wulf White Dec 25 '12 at 19:35
add comment

1 Answer

up vote 10 down vote accepted

You need to have every object positioned relative to the world, not 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. Now 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 now 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;

Now obviously some objects are not actually visible to the camera, so you may want to check if the screen position for any object to be drawn is within the camera's view.

share|improve this answer
add comment

Your Answer

 
discard

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.