Earlier this week, I added some variations to the procedural terrain in Dragon Attack.
Previously, the landscape was generated one segment at a time, forming a “chain” of rotated sprites. Each segment would have the same slope as the previous one, plus or minus a random number. To avoid going off the screen, the random number would be biased downwards near the top, and upwards near the bottom. This system worked great, but it made it pretty hard to implement variety in the terrain. For example, with just the previous height and slope as your “state”, how would you generate a mountain range?
The obvious solution was to switch to fractal noise. Generate an array of random numbers, sample from those at different “frequencies” and with different interpolation functions, and sum them together, each with its own “amplitude”. This gave me lots of creative freedom in varying frequencies and amplitudes (width and height of terrain features) and interpolation functions (smooth or jagged), so I could get a terrain that would look good and play well. It’s also possible to put arbitrary functions in between (for instance, abs or pow) for more dramatic effects.
Taking the chain of segments and making it fit the noise function turned out to be nontrivial. It’s easy to sample the height at some particular x coordinate, but when you rotate the current segment into position, its right side moves, so your sampled height is invalidated. I mostly solved this by sampling multiple times, each time allowing for less rotation to let the thing stabilize. It works well enough.
Lowlands, mostly flat, great for the first level (ignore the headless archers):
Smooth rolling hills, a bit higher and more challenging:
Canyons, created by rounding one of the octaves to a hard 0 or 1 and diminishing their effect far away from the 0.5 range (ignore the artifacts on the edges, they’re gone now):
Mountains, created by sampling multiple times and computing the max, simulating multiple layers and raising the average height in the process:
It’s clear that these need some different sprites as well. Lowlands should have more grass, mountains should have more boulders, etcetera. It would also be nice if things spawned in more sensible places, e.g. no trees above a certain height, and no houses on a steep slope.
I also worked on tree variations and added ballistas. Both need some work still, but I’m sure they will show up here soon.
In other news, Mystery Game No. 1 has a name now! It’s called “Twistago”, and it has been soft launched on Android in the Netherlands, Germany and the Czech Republic. A bigger announcement will follow for the full release, but if you’re curious, you can check it out here. The iOS release will have to wait a bit for reasons of paperwork.