Game Design, Programming and running a one-man games business…

Something I learned today about STL and Z-sorting…

So here is a thing, you might be interested in if you use STL, if you don’t…well sorry :D

if use use the sort()n function thats built into an STL list, it guarantees to preserve the order of identical objects in the list. if you use the vector version, all bets are off.

Bloody hell.

So if you have a bunch of asteroids with these Z values

5,9,3,0,0,0,-2,-5

And you use a list to sort them, all is good in the world. if you use a vector, those 3 asteroids at 0 are going to Z-fight like crazy things.

the solution?

use stable_sort()

well call me mr-picky but I think I’d be happier if stable_sort() was the default, and we actually renamed sort() to be take_your_chances_and_do_random_crap_sort().

I presume stable_sort is slower… Luckily I’m not sorting asteroids every frame (that would be NUTS), and I only sort things when I have to, so it isn’t mega critical. it led to a bug where the biggest hulk chunks from spaceships did Z-fighting if theyu weighed ewnough to all have a Z-speed of zero, and thus a Z position(relative) of 0, so when other objects spinning away caused a z-sort, their order got scrambled. If you are a non coder and don’t know what Z-fighting is, it’s a flickering effect you get in 3D games where two images seem to be undecided about which one is in front. You often see it on ‘decals’ such as blood splats on the floor or posters on a wall. It’s annoying…