SpriteSort with two equal layer depths


I have been messing around with using SpriteSortMode.FrontToBack, but I got some weird behaviour when two draw calls were made with the same layer depth. I used lots of ‘Tiles’ from a tile map and set each of their layer depth as 0.5f. The player also had a layer depth of 0.5f. When the game ran, The player would be under most of the tiles but would be in front of a few as well. The same tiles are behind or on top every call, and they don’t seem to be completely random, as tiles drawn further down the screen tend to be drawn under the player, but this is not an absolute rule. All in all, I’m a little confused with how Monogame deals with equal layer depths, help would be very much appreciated,


Since you say it doesn’t seem completely random, my assumption is that, in the event of equal layer depths, textures are drawn in order of Draw call. So if you had a list of tiles, and iterated through the list to draw them, tiles from the start of the list would be drawn before tiles at the end of the list. If you wanted the player to be consistently behind or in front of the tiles, you could offset the player draw depth by plus or minus .01 or something.

1 Like

This was my conclusion too, but I was still confused at the fact that all of the tiles are still drawn before the player. I iterate through them and draw them, but the player is still drawn after all the tiles are drawn. I’ll mess around with the depth values and see if I can find some logic to it.

I came across this while trying to find something relevant…

Custom depthbuffer - Jagaco Games

This image caught my eye…

Not sure it helps at all just thought it might be something to look at… the custom depth buffer…

Just found this in the docs for SpriteSortMode:

FrontToBack: Same as Deferred, except sprites are sorted by depth in front-to-back order prior to drawing. An unstable sort is used, which means sprites with equal depth may not have their order preserved.
-Enum SpriteSortMode | MonoGame Documentation

Seems like equal depth sprites are an issue. Guess I’ll find a workaround so that equal depths don’t occur. Thanks everyone for your help

1 Like