Tiled world - Z-ordered drawing

I have a tiled world, which is really neat. I have an object for the objects in my tiled world, that doesn’t move and I’m z-ordering them by
casting them in an arraylist by biggest Y-coord, so I can draw the objects accordingly to that and the z-ordering would be correct.

However, I need a player aswell which i’m quite unsure about how to render with correct z-ordering. Do I need to make the player have the same superclass as the other objects, throw them in the same list, and then update that list everytime update() is called? That seems like a bad solution to me… Please give me your input on this, and please ask if I haven’t explained something good enough :slight_smile:

Thanks guys :smiley:

It doesn’t seems too bad to me. I have done it like that several times.
When you say “and then update that list everytime update() is called”, I think you want to say that you need to sort that list every frame. Depending on the number of object in the list (+10000 ;D), it is not a problem. The list is “almost sorted” each time so it may not take a lot of time to sort them (to verify).

Thanks for the reply. 8)
If I’m going to do it that way, how do you suggest sorting them?
I can’t think of a way right now.

A different approach is having a method for drawing the objects, that takes the objects and the player as arguments (different objects), and then loop through the list of still-objects (houses, trees, etc.), but with drawing the player when the loop reaches that x-coord. I can’t see any downsides on that, except complexity, when NPC’s are going to wander my world. That doesn’t seem bad though…
That way all the still objects wouldn’t have to be sorted every frame, but only once while in that scene. However, I still need a way to update the list. I have not worked much with lists - could someone perhaps provide pseudocode just for sorting a list of instances of the objects? :smiley:

My solution is usually to sort every frame. This is actually pretty quick providing you:

  1. place different objects into different layers. ie. the ‘floor’ layer never needs sorting as it’s always drawn first. the ‘object’ layer (with walls and players) needs sorting, but has much less things in it.
  2. first find the visible items, then add them to the sorting list. That means you’re only sorting the 50ish visible characters and walls, not the 1000+ in your level.

I didn’t think of not drawing the objects that aren’t seen - Thanks for that idea. I might go with the sorting in each frame, but how can I do so from a list of all the instances? Do I need to create a new list?

You need a collection of all the objects with their coordinates projected into view space, then you can sort on that (ie with a comparator)

I love tiled games, any screenshots of your W.I.P.?

I can’t do that :frowning: Can you perhaps give a pseudocode example?

Right now it’s not much, as objects are completely left out due to this rendering issue. When that’s in place, smooth walking is my only obstacle :slight_smile:
I’ll supply pictures when I got objects to render with correct Z-ordering.

You can! Read the article and make yourself a selfcontained test case to try it out.

I actually see where this article is going now (had to have a closer look). I’ll read the whole article, when I get home :slight_smile: