Rendering order

Hi!

Let’s say that I have 2D game where I have nice forest which consists of trees (surprise). Every tree would be object and added there using linkedlist or some other nice collection. If those objects are not sorted correctly then it looks like tree behind other tree is rendered in front of the other tree and that looks terrible. I think solution is to sort trees based on their y-values but I cannot get it work.

Is there any simple way to do this?

First you must ensure that the y-value you pick is the bottom of the tree.

Then use Collections.sort(trees, yourOwnTreeComparator) before you render your trees

What kind of view are we talking about? If you’ve got a slanty top-down view (like old 16 bit RPGs like this) then sorting all the tree sprites by their lowest y value should do it (although you should draw your non-overlapping ground tiles first, then your sorted sprites). If that’s not working perhaps post code and screenshots showing the problem.

Yes something like that. It’s not tile based so another tree could be only one pixel lower than other as well it could be treeheight lower.

Indeed that was the first thing that went wrong. I didn’t even think about that.

How can I make my own comparator? ???

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

This is sometimes called rendering by Z-index order.

There is a reason why there is “Z” in the “Z-index”, it’s because it does not use the Y value, or X, but rater an imaginary height value of Z.

Rendering by Y-index is not in my opinion a good way to render trees. In a top-down view the forest will look artificial, trees will always overlap the trees above. You need to randomize the sorting, or e.g. randomize the Z-index, for it to look more natural.

If the sprite of the tree is top-down (you can’t see the trunk), you’re right.

If the sprite is front-to-back (thrunk low, leafs high) there is no need to randomize, in fact, randomizing will mess up.

A topdown game with sprites with a frontal view is like isometric perspective, where ‘depth sorting’ follows the same rules as in true 3D.

You can also just be smart beforehand about what order you’re adding your trees to your list/array, and then just go straight through the list. I’ve always had my map editor do that and it worked fine.

In that case, you’d have max insert-speed with a SortedSet like TreeSet.

If your scenery is static (and you can’t rotate the view!) a List or plain array will perform better.

Yes, I should have mentioned that doing the above only works if you have a static background, although because you said that your game was like Chrono Trigger I figured that was the case.

I usually have a 3D array that represents background elements (3D so I can have multiple layers), and nothing in the array can be moved around or updates. Then I have some sort of list that represents all entities that can act and change, which depending upon what you’re doing may or may not need to be ordered. Most top-down games don’t really need to have the dynamic entities stacked correctly, so you can just not worry about it.

Ok, thank’s for you replies.