Friday, December 5, 2008

Procedural Generation

Creating a landscape can be a time-consuming task. Because of that, it is interesting to consider procedural generation, where you let a script do the work for you. Classic examples of procedurally-generated terrain are fractals; they are a simple but effective way to create realistic-looking landscapes, and also things like trees.

As the Intensity Engine is already integrated with Python, it was a natural idea to script the generation of terrain using that language. So, here is an example of a giant spiral generated by Python:


That's what it looks like from the ground, and this is what it looks like from above:



This is generated by a short Python script, basically just to implement the equation for the spiral, which is something like x = r(t)*sin(t), y = r(t)*cos(t), z = h(t), where r(t) is a radius, r(t) = a*t+b, and h(t) is the height, h(t) = c*t. t goes from 0 to 1. It's pretty much that simple.

The real work was in processing the output of the Python script in C++. This is done by a general algorithm (not specific to the spiral - it can be used for anything) that generates appropriate Sauerbraten cube geometry from the raw data, and then applies some smoothing to the result to make it look more natural. The code appears in editing_system.cpp if you are interested.

Using that code, one can now create a lot of different types of scenery automatically. I have so far made some examples, of a sphere (which isn't perfectly round - it needs more smoothing) and staircases. These are the sort of things that can be tedious to do by hand, that Sauerbraten mappers currently spend a lot of time on. In fact it is possible (minor tweaking might be needed though) to perform procedural generation in the Intensity Engine and then import that map into Sauerbraten, which would open up a lot of interesting possibilities for Sauerbraten mapping (contact me if you're interested and I'll help out). The code might also be ported to Sauerbraten, but that would not be trivial as it relies on some Intensity Engine specific features (Python integration and loading of maps on the server).

A video of the spiral, and it's creation process, can be found on the screenshots and videos page.

No comments: