Sprite render order using libgx tiled maps

Okay, so I am officially stumped,

I am making a 2D top-down game using libGDX. I make my tiled maps using TILED.
Is there a way to order/sort the map objects so they display in the correct z-order on a specific object layer.
All I really need is for the one entity to look like they are standing in front of another entity on the same object layer.

I overrode the BatchTileMapRenderer so that the map object (which I made from another Entity class) are actually displayed when you render the map


public class MapRenderer extends BatchTiledMapRenderer{
    @override
    public void renderObject(MapObject object) {
        if(object instanceof Entity) {
            Entity ent = (Entity) object; ent.render();
            batch.draw(ent.sprite, ent.pos.x, ent.pos.y);
        }
    }
}

This is literaly all there is to it to render the map itself


    public void render(Camera camera){
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();
    }

The reason I want to do it this way is to keep it simple, and to maintain the z-order of tiles,
where if the player walks under a bridge the bridge is displayed above the player.

Any and all help would be welcome… even if to just tell me I’m doing it wrong
Many Thanks.

One idea could be creating “next iteration” list (which still will render in the same rendering phase). While iterating over you main list, you check the z of the object and if it is not the current level, you place it in the “next iteration” list. When finished with current list, you start by drawing tiles from “next iteration” list (if needed you can perform the checks and build yet another “next iteration” list). You can think about it in the way that you reuse exiting list initialization (in order to reduce the amount of objects for GC to dispose). The main advantage in terms of the most simple solution (which would be iterating the initial tile list over and over again) is the amount of iterations you might need to do, since if you have just few bridges, the “next iteration” list will have very few components.

That’s something I didn’t actually think about, I will check it out. Although there will be on average 100+ entities or map objects to draw and each entity would need to look as though they could be standing in-front of any other entity, wouldn’t that have a massive negative impact on performance?

Also when I say maintain the z-order of the tiles I meant that I have 3 layers in the TILED map, 1 for floor tiles, 1 for entity map objects and 1 for bridges so to say. libGDX renders it in the order of the layers to make it appear as though the entities are on the floor yet under the bridge. I should probably of made that part more clear.

MapObjects are rendered in the order they are added. In my opinion, libGDX’ existing tile map renderers are, out of the box, simply unusable if objects need to consider any order of rendering.

My advise would be: override TiledMapRenderer.renderObjects(). Keep the MapObject array, or use some sensible (spatial) data structure to store all objects on the map. Each frame, iterate objects, perform view culling, extract the objects visible on the screen to a second array. Sort this array by object.position.y. Render visible objects top to bottom.

I use a custom made batch renderer for z-ordering:

It does not work with libgdx tiled maps now, but maybe it can give inspiration.