# How do I Z-sort triangles for a 3D engine?

**URL:** <https://forum.kirupa.com/t/how-do-i-z-sort-triangles-for-a-3d-engine/314110>\
**Category:** Uncategorized\
**Created:** [September 18, 2010, 2:16pm UTC](https://forum.kirupa.com/t/how-do-i-z-sort-triangles-for-a-3d-engine/314110 "2010-09-18T14:16:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeldalink](https://avatars.discourse-cdn.com/v4/letter/z/94ad74/32.png) [@zeldalink](https://forum.kirupa.com/u/zeldalink)\
**Post date:** [September 18, 2010, 2:16pm UTC](https://forum.kirupa.com/t/how-do-i-z-sort-triangles-for-a-3d-engine/314110/1 "2010-09-18T14:16:25Z")

</div>

I’m building a small 3D engine for a game I’m working on. I’ve got my basics sorted: textured triangles with backface culling. However depth sorting is proving to be a difficult problem.

I’m calculating the face Z by averaging out the 3 points that make up the triangular face. The longer faces sometimes overlap the smaller faces since they have a larger Z value and therefore rise up in the depth sorted display list.

How do I fix this?

Is there a way I can use the area of the polygon to influence the depth sorting in any way? Or are there algorithms to accurately detect which triangle is above which triangle?

**Cathedral rendered in a 3D program**

![](http://forum.kirupa.com/uploads/kirupa/6339/68e6ae5df03f926c.jpg)

**Cathedral rendered in my 3D engine**

![](http://forum.kirupa.com/uploads/kirupa/6340/32ded13e27a65c58.jpg)
