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

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 = closer to camera.”

Fast Facts

AspectDetail
Also calledPainter’s algorithm (2D), Y-sorting
Problem solvedCorrect sprite overlap
Key genresBeat ‘em ups, action RPGs, isometric
Modern equivalentZ-sorting, depth buffer

The Core Principle

In top-down perspective:

Screen PositionDepth Interpretation
Higher Y (top)Further from camera
Lower Y (bottom)Closer to camera

Objects closer to camera should draw last (on top).

Sorting Approaches

MethodSpeedBest For
Bubble sortSlowFew sprites, nearly sorted
Insertion sortFastNearly sorted (most frames)
Bucket sortFastFixed Y precision
Radix sortFastMany sprites

Implementation Considerations

FactorImpact
Sort keyUsually sprite’s foot Y position
Tie-breakingX position or object ID
FrequencyEvery frame (positions change)
StabilityPrevents flickering

Platform-Specific Challenges

Hardware Sprites (C64, NES)

ConstraintSolution
Fixed prioritySprite multiplexing with Y-sort
8 sprites/lineCareful positioning
Priority registersSet based on sorted order

Bitmap Rendering (Amiga, PC)

ConstraintSolution
Draw order mattersBack to front rendering
TransparencyMust respect sorted order
PerformanceMinimise overdraw

Beat ‘Em Up Example

In a side-scrolling beat ‘em up:

ObjectY PositionDraw Order
Background enemyY=80Draw first
PlayerY=120Draw second
Foreground enemyY=160Draw last (on top)

Optimisation Techniques

TechniqueBenefit
Insertion sortNearly sorted = O(n)
Dirty flagOnly sort when needed
Spatial bucketsPre-group by screen region
Index arraysSort indices, not objects

Edge Cases

SituationSolution
Same Y positionUse X position as tiebreaker
Large spritesUse anchor point (usually feet)
Jumping charactersSort by ground position, not sprite Y
Flying objectsSeparate layer or shadow position

Modern Applications

ContextUsage
2D games in UnitySorting layers + Y-based order
Isometric gamesHades, Diablo use Y-sorting
Transparency sorting3D with alpha still needs this
Tile-based gamesObject-over-tile ordering

Legacy

Y-depth sorting remains fundamental—modern engines automate it, but understanding the principle helps debug visual layering issues and optimise rendering in sprite-heavy games.

See Also