# How do I handle game objects with two depths?

I want to be able to do what you see in the image, but with a single wall. The player should be able to walk on either side of the same object. That is, the same wall is below the hero when I go down from it, and it's above the hero if I come from above it.

It's like two depths in the same wall, so its not just a matter of drawing it before or after. I could cut the object in two, but that would be the easy (and maybe computationally expensive) solution. How should I do this?

• Spliting the object in two seems the most natural way of doing it. The upper part is behind the character, and the lower part is between the player and the character. Still not computationnaly expensive, you just have to make sure both part don't go away from each other. – Fabinout Jul 5 '13 at 8:52
• Ye i most likely will have to do so, but any other ideas? – Xkynar Jul 5 '13 at 15:34

Here are three ways to do it. Which one will be suitable depends on what kind (graphically) of game you're making.

1. As user Fabinout already commented: “Splitting the object in two … The upper part is behind the character, and the lower part is between the player and the character.” If you are making a rigidly tile-based game (the shown paths are the only two possible paths near the wall), this is likely a fine way to do it. Note that for this to work, the distance between the upper and lower paths must be at least as much as the height of the character.

A disadvantage of this method is that it only helps with cases like this particular one; it's not a general strategy for stacking sprites.

2. Draw all sprites — both the player and the scenery — from top to bottom in order of their “ground level” positions (base of the wall, player's feet). This will automatically cause the wall to be shown on the correct side, and is a common strategy for “isometric” and “2D RPG” styled games.

Another advantage of this is that it generally looks good even if objects pass through each other — e.g. characters that don't block others' movement; walking through a field of tall grass. Disadvantages are that you need to efficiently determine the drawing order (e.g. either sort your sprites, or have an 2D array of tiles which know what sprites are in them) and that it gets complicated when you have objects which are not all visually rooted on the same ground level.

3. Have a “front layer” and “back layer” where your character occupies one or the other (and the wall is drawn after back but before front). Depending on how you approach the wall, the character gets switched to the front or back layer by trigger zones. This is how Sonic the Hedgehog games handle things like loop-the-loops.

The advantage is that you can have essentially two independent 2D planes to build your level elements on. However, this is more appropriate for a game where vertical motion is jumping up, whereas from the shape of your graphics, your game appears to be one where vertical on-screen motion is moving front/back on the ground, and there are far more than two different positions in that dimension.

• Using layers like #3 is how many, if not most old consoles' side scrolling trickery worked. – Patrick Hughes Mar 19 '14 at 5:37

Draw objects in order of the Y value, so objects "further away" (higher up on the screen) are drawn before/underneath "closer" objects. That simple.

More generally, just give objects a depth value (which might be implicitly based on Y or explicitly set, depending on game and needs) and render in that order.

This is the "painter's algorithm" at work.