Right now I'm working on a small 3D game engine. I've been having some problems with sorting my objects so they draw in the right order. All my objects are meshes made of triangles. Here's a couple screenshots of the program:

Here's some of my code. I sort all the triangles based on what they return from these functions:

This finds the average position of the triangles on the Z axis:

Code: Select all

` def z_avg(self):`

#return (self.coords[0][2] + self.coords[1][2] + self.coords[2][2]) + self.center

dist = 0

for corner in (0,1,2):

dist += self.coords[corner][2] + self.center[2]

return dist

This finds the triangle's average distance from the camera (0,0,0):

Code: Select all

` def distance(self):`

dSquared = 0

for axis in (0,1,2):

d = (self.coords[0][axis] + self.coords[1][axis] + self.coords[2][axis])/3.0 + self.center[axis]

#d = max(self.coords[0][axis], self.coords[1][axis], self.coords[2][axis])

dSquared += d**2

result=dSquared #sqrt(dSquared)

return result

If someone has any insight on z sorting, advice would be appreciated.

Elyon's Strength,

Nathaniel