SVG 3D depth sorting mathematics

Hello, I recently adapted some of the info from the Flash 3D tutorials in making an [URL=“http://en.wikipedia.org/wiki/Scalable_Vector_Graphics”]SVG (Scalable Vector Graphics) 3D game engine. There are plenty of problems, especially from the performance and specification support standpoints, but I’m taking this challenge on anyway. It’s built for Opera 10.

The engine utilizes a first-person perspective and currently the player can only move in the X and Z axises. I plan to add Y axis movement (jumping, climbing) as well as two-axis rotation (player view follows the mouse cursor).

One problem that I have is depth sorting. SVG renders the newest drawn shape on top of others. This behavior is not fundamentally a problem for me, but determining the drawing order is. I need a purely mathematical way to sort shapes based on their orientation/distance to the camera. Imagine you are looking at the closed door to some room. You can’t see past the door and into the room because the face of the door (a polygon) is inhibiting your view of objects inside the room (other polygons). In my engine, the room’s polygon’s would be drawn first, and then the door (this could be inefficient, but at the least it looks correct).

Previously I reasoned that calculating the 3-dimensional distance from the center of a polygon to the camera coordinates (a single point) and then ranking the rendering order based on the distances (farther stuff drawn first) would result in a correct sort. I was wrong and I was able to work out by hand instances in which this was incorrect.

I do not believe the problem is due to the camera having a Y of 0, but I could be wrong.

Anyway, I would be grateful if someone could shed some light on this or provide a magic formula that I have overlooked. If you need to look at or are interested in my SVG engine code, I would be happy to provide it. After/if I finish this thing, I’d like to GPL it so that anyone can use it and it could serve as a test of SVG performance.