1
\$\begingroup\$

Say you're rendering a 2D top-down tile map and there are some objects in the tile map that the player should be "in front" of when below them, and "behind" when on top of them. Like in this example screenshot:

enter image description here

This works because the player is on layer 1, the top two tiles are on layer 2, and the bottom tile is on layer 0.

Let's say in this example that the object has no collision. If the character then walks up, then the character will be half-in-front and half-behind, like this:

enter image description here

The reason why this is a problem is because with tiles, they don't really have a concept of state. If the 3 tiles were part of the same "object", then this wouldn't be a problem, because we could just render the entire group of 3 tiles as a single sprite, and then sorting could be done, and the entire sprite could be rendered behind or in front of the player.

However, with tiles, this knowledge isn't known, as they are purely decorative numbers.

One possible solution is just to instead forsake tiles for these sorts of objects, and force them to be entities in the game itself as opposed to just tiles, but because there can be many of these, that would be a performance hit. It also complicates the logic.

Is there any better solution?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This problem is usually worked out by collisions. Hopefully your tile editor allows you to set groups of tiles together along with their collisions, so you don't forget to set them. And that's it.

However, there might be parts of a game where you need to support something like this, and there is a way to do it that is not making everything entities…

As you know, if you use multiple layers of tiles, you can have some be drawn before the entity, some after.

Now, a few new ideas:

  • An "object" must exist entirely on a layer.
  • There must be layers exclusive to entities.
  • The layer on which an entity is, can change.

If each "object" exist entirely on a layer, and entities are never on that layer, then entities are either drawn "behind" or "in front" the "object".

How do you decide the layer of an entity? You annotate layer constraint on a map (e.g. "entity layer must be greater than x"). You can think of it as the "ground layer" for a given position. And use that to constraint the layer of an entity based on where it is. That is, when the entity moves, you check the constraints at its position, and move the entity to a layer that satisfy them.

For the example depicted in the question, you would have layer 0 be the ground, layer 2 be the object. And, depending on where the entity is, it can be on layer 1 or layer 3.

Now a couple things:

  • The idea of annotating constraints, and not simply what is the layer for entities, is to allow for flying entities to go above. If an entity is not a flying entity, it would just pick the lower layer that satisfy the constraint.
  • You don't have to annotate this constraint on all tiles, only on those that need to make the entity change. In fact, you may not need a custom system for these constraints, as many systems allow you to set triggers on tiles.

Consider for example, a bridge. Entities can walk either above or below it. Depending on the direction from where they approach it. For that case, you can have constraints that set the layer of the entities appropriately on the sides of the bridge. You can even have triggers on the edge of the bridge that change the layer and play a "jump down" animation, only for entities that were top side, of course. And yes, if you are using a system where you need to set triggers per tile, that can be a lot, thus probably not a feature of every map.

\$\endgroup\$

You must log in to answer this question.

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