Rendering order in a 2d game

Greetings JGO.

I’m currently making a 2-dimensional game in java using lwjgl. It contains objects(such as trees) which should cover whatever they stand in front of(determined by their y-coordinates). This is done to create the illusion of a 3rd dimension. Example seen in this image from the game, where the player is placed ‘behind’ the tree:

I am however adding entities to the game(mobs, items etc.) and these are stored in a list.
My question is: How do i figure out in what order to render the list of mobs/entities and the trees?

Isn’t it just the bottom of the sprite?

In your picture, the bottom of the tree is below the bottom of the person, so the person gets rendered first. That way you stand “behind” things that are closer to the “camera”?

Just to clarify. You have a list of Objects (Trees), and you want to render mobs/items so they go behind the tree?

This is assuming that your mobs/items are only one tile tall :
Render Order :

  • Tiles
  • Items
  • Mobs
  • Player
  • Trees

To get it you can have a mob that is greater then 1x1 you need to have all your render objects in one list, then sort the array by the y values(Greater values first, then the lower values last)

(Its a bit late so correct me if I’m wrong)

How would you want it to look in that picture if the person was standing on the tile immediately south of the base of the tree trunk? I mean you’d expect him to appear in front of the trunk then, but if the tree has one “height” applying to the whole tree, the trunk would be rendered in front of the person’s head but we’d see his legs. It’d look odd. Perhaps there could be a separate object for the tree trunk and the crown? Not sure really.

If objects can cover several tiles it might be better to go with a system where big objects were constructed from 1-tile subparts, each with its own height. Then it might render more realistically, plus you might also be able to do collision tests based on height, e.g. if a 2-tile tall guy tries to walk through a door that’s only 1 tile high. But maybe I’m overthinking it :smiley:

[quote=“Magn,post:1,topic:51095”]
Generally you just loop over the list of entities a number of times (once for each layer) and render the entities that are part of that layer. The entities rendered in earlier loops are drawn under the ones in later loops. You could, for example, add an integer “layer” property to your entity class and use that to determine what render loop the entity is rendered on, and thus in what graphical layer it will appear.

Thank you for the answers. But I don’t think I explained it well enough, sorry about that.
The picture is just a demonstration of the effect I want to achieve where the player stands behind the tree.

I have an arraylist of mobs(entities) with different x and y positions. Now I want those with a lower y position to be rendered first(so that they appear to be behind the others) but since it’s a list they are not in any particular order.
So my question is how do i calculate in which order the mobs should be rendered?
I thought of sorting the list by y-value using some algorithm, but then I would have to sort everytime a mob moves(which could be up to 60 times a second).

Thank you.
That is the way i thought of doing it, but wouldn’t that be slow if I have to check the y-positition of every mob several times per render?

We really can’t tell you whether it’s going to be “slow” on your system with your code. Just try something and see if it works. Sorting using a custom Comparator and seeing what happens will be much faster than waiting for somebody else to explain it to you (especially since I think I saw you ask this question on another forum a couple weeks ago). Then if you notice some performance impact, we can start talking about fancier alternatives.

Get something that works, even if it’s slow. Then post that as an MCVE and we can start talking about improvements- if it even needs any.

“Premature optimization is the root of all evil.” - Donald Knuth

Okay thank you sir, I will do that. (But I did not ask this before btw :))

Huh, right you are. This guy just had the same question as you: http://www.javaprogrammingforums.com/whats-wrong-my-code/39468-tiled-map.html

Sorting things every frame is no problem, it can be done even in fairly extreme cases: http://www.java-gaming.org/topics/cache-locality-and-libstruct-fixing-java-s-horrible-memory-layout/33231/view.html

AgentD is (or was? I hope he still is) making a game where there are ~100K spaceships flying around at any given time, and they are sorted each frame to do collision. Crazy! So a few dozen isn’t a problem at all.