# How does a 2D game like Zelda handle the character being behind buildings where only part of him is shown?

I'm trying to develop a simple Zelda-style game. I found this very useful topic

How did LoZ: A Link to the Past handle sub-tile collisions?

that I'm using as reference to develop my collision detection mechanism. However, there's something I've failed to understand. When Link is behind buildings, only part of him is shown. Can anyone please help me understanding how this effect can be achieved? Isn't the building part of the background?

• I like to think of the render style as having a ground layer (grass, tile flooring, etc), an object layer (pots, enemies, shurbs, link, etc), and a sky layer (roof tops, lifted pots, tree tops, etc). The logic for moving things between layers can be a bit weird in the example of pots, but that another matter. – Benjamin Danger Johnson Apr 15 '13 at 22:27

2D Tiled games usually have more layers which renders in different layers - some renders in the background others above the player.

That part of the roof renders above the player and that's why player can go behind that building.

Here is the image example from my game to explain better:

• So the building itself can be a part of the background, but the roof is part of a different layer drawn on top of the layer with the sprites? – Tinadm Apr 15 '13 at 21:50
• @Tinadm it doesn't necessarily have to be the entire roof, it could just be the section of roof that would draw on top of the player that's rendered after everything else. – Tetrad Apr 15 '13 at 21:53
• +1 for awesome graphics. Really helps the visual learners. – UnderscoreZero Apr 15 '13 at 21:56
• I'm pretty sure old SNES JRPGs used the same technique. FF2, FF5, etc. The technique is still valid. – Hatoru Hansou Apr 16 '13 at 9:44
• You can see this pretty well with emulators, e.g. ZSNES, which allow you to manually switch individual layers off and on. – Tobias Kienzler Apr 16 '13 at 11:27

While Blodyavenger's answer covers how it is most likely done, here's another possibility which would allow for easy perspective switching (e.g. four or eight different directions, or some camera elevation levels):

Consider the top-down view of your map, e.g.

  ABCDEFGHIJKL
1 +-----=----+
2 |  .    .  |
3 |    X   o |
4 | .        ||
5 |    .   . |
6 +--=-------+


Where, say, X marks a high pillar. For each object you store images of all possible perspectives (or use the same...), which may be arbitrary many tiles high. Now when drawing, simply start with the column or row that is on the back in the current perspective, e.g. row 1 if looking from south. After that draw the row/column the second-most to the back and so on. This way, when something is higher than one tile (stretched to the current perspective), it will simply overdraw what lies behind.

(Sorry for the lack of nice pictures, I'm not too good at applied graphics...)

• Note how you can easily change the camera elevation this way by stretching the object textures – Tobias Kienzler Apr 16 '13 at 12:04

You should have a variable for depth , and have the images with biggest depth drawn first , then the others with lowest depth. And the depth of each object should equal the negative of its y coordinate.

Depth=-y