Skip to content
Techniques & Technology

Y-Depth Sorting

Sprite ordering by position

The technique of sorting sprites by their Y position to create correct depth ordering in top-down and isometric games, essential for beat 'em ups and action RPGs.

cross-platform spritesrenderingsortingbeat-em-upisometric 1984–present

Overview

Y-depth sorting is the technique of ordering sprites by their vertical position so objects lower on screen appear in front of objects higher on screen. This creates correct visual layering in top-down and isometric games where "lower on screen = closer to camera." It's the simplest depth-sorting method that works — older than the formal painter's algorithm (1972) in arcade game form, made standard practice by Marble Madness, Knight Lore, and the early beat-em-up genre.

Fast facts

AspectDetail
Also calledPainter's algorithm (2D), Y-sorting, depth-sorting
Problem solvedCorrect sprite-overlap ordering when sprites can overlap
Key genresBeat-em-ups, action RPGs, isometric, top-down adventure
Modern equivalentZ-sorting, depth buffer, transparent-sort layer in modern engines

The core principle

In top-down or 3/4 perspective, the screen-Y axis maps directly to camera-distance:

Screen YDepth interpretation
Higher Y (top of screen)Further from camera
Lower Y (bottom of screen)Closer to camera

Objects closer to the camera should draw last (so they overlap things further away). Sort sprites by Y ascending, then draw in that order. The object lowest on screen draws on top.

Sorting approaches

MethodTime complexityBest for
Bubble sortO(n²) worst, O(n) bestFew sprites, nearly-sorted (last frame's order)
Insertion sortO(n²) worst, O(n) bestNearly-sorted — most frames don't reorder much
Bucket sortO(n + k)Fixed Y precision (e.g. one bucket per scanline)
Radix sortO(n × digits)Many sprites, fixed-width Y values

Insertion sort wins in practice for game sprites — most frames have only a few objects swap order, so insertion sort runs near O(n).

Implementation considerations

FactorImpact
Sort keyUsually the sprite's foot Y position (where it touches the ground), not the sprite's centre or top
Tie-breakingWhen two sprites share the same Y, use X position or object ID for stable ordering
FrequencyEvery frame — positions change every frame
StabilityStable sort prevents flicker when sprites have identical Y

Platform-specific challenges

Hardware sprites (C64, NES)

Hardware-sprite systems have fixed sprite priority slots — sprite 0 always draws over sprite 1, etc. To Y-sort, the game must shuffle which graphics go in which sprite slot:

ConstraintSolution
Fixed slot priorityMove sprite graphics between slots based on Y order
8 sprites per scanline (NES)Combine Y-sort with sprite multiplexing
Priority registersSet background-priority bits based on sorted order

Bitmap rendering (Amiga, PC, Spectrum)

Software sprite systems draw sprites in any order, but the order matters absolutely:

ConstraintSolution
Draw order mattersRender back-to-front; back sprites drawn first, front sprites drawn last
TransparencyEach sprite must respect those drawn before
Overdraw costFar objects may be obscured — minimise overdraw via culling

Beat-em-up example

In a side-scrolling beat-em-up like Final Fight or Streets of Rage:

ObjectFoot YDraw order
Background enemyY = 80Drawn first
PlayerY = 120Drawn second
Foreground enemyY = 160Drawn last (on top)

When the player walks behind a barrel, it's because the barrel's Y is higher (further down the screen) than the player's. Walk in front: the player's Y is higher. Same code, different positions.

Optimisation techniques

TechniqueBenefit
Insertion sort with prior orderNearly-sorted on most frames; near-O(n) typical
Dirty flagOnly re-sort when something moves enough to change order
Spatial bucketsPre-group by screen region; sort within buckets
Index arraysSort an index array, not the objects themselves; preserves object data layout
Bucket per scanlineUltimate-Y-precision spatial bucket; common in 16-bit games

Edge cases

SituationSolution
Same Y positionUse X position as tiebreaker (left-most first) or stable sort by object ID
Large spritesUse anchor point — usually the sprite's feet, not its centre
Jumping charactersSort by ground Y (where the character would land), not the sprite's current Y while airborne — keeps the character behind the same objects when jumping in place
Flying objectsEither separate layer (always-on-top) or use the ground-shadow's Y
Multi-tile objectsUse the closest-to-camera point (the bottom edge of the object)

Modern applications

ContextUsage
Unity 2DSorting layers + Y-based custom axis on the Sprite Renderer
Godoty_sort_enabled on Node2D containers
UnrealCustom Z-axis or sort group with Slate UI
Isometric gamesHades, Diablo — Y-sort within the isometric tile
Transparency sorting (3D)Even with depth buffers, transparent objects need explicit back-to-front sorting

Legacy

Y-depth sorting remains fundamental — modern engines automate it via sorting layers and built-in features, but understanding the principle helps debug visual layering issues, optimise rendering in sprite-heavy games, and properly sort transparent objects in 3D rendering pipelines. Every game with a "weird sprite layering bug" has eventually traced it back to Y-sort logic.

See Also