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
| Aspect | Detail |
|---|
| Also called | Painter’s algorithm (2D), Y-sorting |
| Problem solved | Correct sprite overlap |
| Key genres | Beat ‘em ups, action RPGs, isometric |
| Modern equivalent | Z-sorting, depth buffer |
The Core Principle
In top-down perspective:
| Screen Position | Depth Interpretation |
|---|
| Higher Y (top) | Further from camera |
| Lower Y (bottom) | Closer to camera |
Objects closer to camera should draw last (on top).
Sorting Approaches
| Method | Speed | Best For |
|---|
| Bubble sort | Slow | Few sprites, nearly sorted |
| Insertion sort | Fast | Nearly sorted (most frames) |
| Bucket sort | Fast | Fixed Y precision |
| Radix sort | Fast | Many sprites |
Implementation Considerations
| Factor | Impact |
|---|
| Sort key | Usually sprite’s foot Y position |
| Tie-breaking | X position or object ID |
| Frequency | Every frame (positions change) |
| Stability | Prevents flickering |
Hardware Sprites (C64, NES)
| Constraint | Solution |
|---|
| Fixed priority | Sprite multiplexing with Y-sort |
| 8 sprites/line | Careful positioning |
| Priority registers | Set based on sorted order |
Bitmap Rendering (Amiga, PC)
| Constraint | Solution |
|---|
| Draw order matters | Back to front rendering |
| Transparency | Must respect sorted order |
| Performance | Minimise overdraw |
Beat ‘Em Up Example
In a side-scrolling beat ‘em up:
| Object | Y Position | Draw Order |
|---|
| Background enemy | Y=80 | Draw first |
| Player | Y=120 | Draw second |
| Foreground enemy | Y=160 | Draw last (on top) |
Optimisation Techniques
| Technique | Benefit |
|---|
| Insertion sort | Nearly sorted = O(n) |
| Dirty flag | Only sort when needed |
| Spatial buckets | Pre-group by screen region |
| Index arrays | Sort indices, not objects |
Edge Cases
| Situation | Solution |
|---|
| Same Y position | Use X position as tiebreaker |
| Large sprites | Use anchor point (usually feet) |
| Jumping characters | Sort by ground position, not sprite Y |
| Flying objects | Separate layer or shadow position |
Modern Applications
| Context | Usage |
|---|
| 2D games in Unity | Sorting layers + Y-based order |
| Isometric games | Hades, Diablo use Y-sorting |
| Transparency sorting | 3D with alpha still needs this |
| Tile-based games | Object-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