To Infinity and Beyond…

I was writing stories about secret places and fantasy worlds. I was 8 years old. It all started there and then.

And now, I'm a magician. I can literally generate these worlds through my fingertips. Typing on a keyboard, sending instructions to a computer.

VR is incredible: you can literally take a world that only exists in your head and vomit it into a computer simulation. And before you know it, you're physically transported to that world, it becomes reality (sort of).

And after toying with this idea for years, I finally got down to it.

Let the Machine Do the Work

I'm talking about getting a computer to create worlds for me, it's called procedural generation. It's brilliant. Once you've written the code, you can generate unique content through click of a button. Press play and out pops a new world!

But here's the thing. It's not a new concept, it has existed for many years. It was first used in the late 70's, in titles like Beneath Apple Manor and Rogue, where dungeons would be generated procedurally, using ASCII characters (those were the days...).

It was famously used for the Elder Scrolls 2: Daggerfall, which offered a massive world to explore, generated procedurally at runtime. And in the Elite series, where you can explore billions of planets. Without mentioning No Man's Sky and Minecraft...

It's used everywhere. To generate interesting textures, create 3D models, like trees, even generate new sounds and music. Or to spawn loot in MMOs and create unique guns in the Borderlands series.

Let's cut to the chase.

Project Infinity

Procedural Generation

As a small project, I wrote C++ code running in Unreal Engine 4 (UE4). It creates a terrain and populates it with objects. I called it Project Infinity (I know...).

How does it work?

First, we create the terrain, by generating triangles through code. We specify the position of three vertices and generate a mesh at runtime. 2 triangles together make a square (tile) and by glueing squares together, we can create a flat terrain.

Then, we create random height variations for each tile. That way, we generate unique bumps and hills each time, and voila, we've got a procedural terrain generator. I skipped some of the details to keep it short, but that's the idea.

Now, an empty terrain is pretty boring. So we'll add some trees, grass and rocks. That's the population part of the process. The idea is to take the terrain we created in phase 1 and split it in small squares. And in each square, we will spawn an object randomly, from a list of objects that we give as an input.

For the splitting of the terrain, we first decide if we should split the whole terrain horizontally or vertically. It's decided based on a randomly generated number.

Then, we take one of the two parts we've split and we follow the same process over and over until everything has been split. Then we do the same for the other part. Whether we should split further each new square is also determined by a random number and a diminishing probability as the squares become smaller. So we end up with variations in squares sizes and a nice random spawning of objects.

Finally, to place the objects properly on the terrain (at the right height), we generate a navigation mesh over the terrain and spawn objects over that mesh (project to the mesh), hence they will follow the slope of the landscape.

Here's the final result in a video.

 

Here I'm changing the values of some key elements, like the chance to split squares into smaller parts: increasing the split chance will generate more square, resulting in more objects being spawn, a denser population. The other parameter is the maximum steepness of the slope, which results in a more or less hilly terrain.

Other parameters will determine the size of the terrain, the maximum height of hills or troughs (potentially steep slope, but hills of a limited size) and of course which 3D objects to spawn (they could be houses or even characters).

Where is this going?

Of course, we could take it a lot further. You can use procedural generation to build houses or whole cities, create dungeons, grow trees or even create patrol routes for randomly spawned characters/enemies. So you can literally generate a fully living environment on the fly.

As the demand for virtual spaces and for content in general is growing, this is a space worth investigating and watching. I can't wait to see what comes next and I'm sure I'll be exploring it further myself in the future.

P.S.: I want to thank Stephen Ulibarri and his Druid Mechanics channel. His procedural generation series got me started on the terrain population side.