Sep 06
Little bits of speed
Not a lot of coding time today, so I just tackled a few speed issues that had arisen in the last few days.
The major one is that now that full procedural objects are being baked together into a single mesh (instead of lots of individual sub-meshes), the process of optimising those super-meshes was very slow. The worst-cast situation at the moment is undoubtedly the starting area building, which is made up of about 600 triangles in total, or 1800 vertices, before being optimised down for rendering. (I know, this doesn’t sound like a lot of triangles or vertices. But due to its focus on procedural geometry, MT2 has to do a lot of model processing during runtime which other games can do outside of the game)
Optimising these procedural models mostly consists of checking all those generated vertices, to see if any of them are close enough together to be merged together. This means that for every element in the list, you need to check it against every other element in the list. And when you have 1800 vertices, that’s an awful lot of checking. Computer science folks call this O(n^2). I just call it “really, painfully slow”. Building that model freezes MT2 for about three seconds, on my computer.
What I’ve done is to add a simple cell structure to the mesh optimisation step. Basically, imagine that the model is being built inside an 8x8x8 grid of cubes. This way, vertices don’t have to be checked against every other vertex in the model — they only need to be checked against other vertices which are located in the same cell that they are. This optimisation has sped up the creation of the “starting point” model from about three seconds to about half a second. Which is still much too long, but it’s an awful lot better! I’m going to have to think more about how to improve the speed further.

September 7th, 2010 at 3:30 pm
Your cells are similar to an optimization technique often used in raytracing.
It gets more efficient when each cell is itself broken into smaller cells. If checking a cell is computationally cheaper than checking a point, you can build up an acceleration structure for your geometry and get O(log n) time (search for oct-trees).
September 7th, 2010 at 4:47 pm
Yeah, Tycoon2 uses quadtrees and octrees quite extensively to perform render culling, collision testing, and many other functions. In this case, though, since I’m only wanting to test points against points in the same position, there’s no real benefit to the hierarchical structure that they give you.
But yeah, they’re fantastic for all sorts of purposes. Definitely one of those techniques it’s worth keeping in your repertoire!