Procedural Generation – A Comprehensive Guide Put in Simple Words
Hey there!! How are you doing?
Welcome to 8bitmen.com
This article is a comprehensive write-up on procedural generation. A really cool technique of algorithmically generating content with minimal effort.
By the end of this article, we’ll be able to answer all the questions about it such as what is it? Why use it? When should we use it? Algorithms required to procedurally generate content & all that jazz. I will also talk about learning resources, tutorials, techniques to move forward with it.
So, without any further ado let’s get on with it.
1. What is Procedural Generation?
Put in really simple words, procedural generation means the data or the content is generated via a certain logic/algorithm, instead of a human creating the content manually.
This technique can be applied to n number of fields such as gaming, mathematics, animation, computer graphics, 3D modelling, movie editing, artificial electronic music etc.
Now let’s understand this with the help of an example.
What if I ask you to write a series of numbers, starting with 1 & adding 2 to every number till we reach 1 million.
Now the obvious manual way to do is write 1 on the paper & keep writing the number in the series, mentally adding 2 to every number till we reach 1 million. Now we can naturally imagine the time it would take.
What if we have to do this thing over and over in our project?
Oh!! God no. I already feel groggy…
A really fun alternative way of doing this is, we write a program to execute this task for us. Put the counter at 1, tell the computer, look buddy here is the logic you just have to add 2 to every number & keep procedurally generating the series until you reach a million. That’s it.
And the computer will generate the series for us.
Voila!! See how useful & time saving this technique can be.
Well, this is a really primitive example of how procedural generation can be applied to things.
Procedural generation is put to use in a lot more complex ways like generating graphics for video games. The awesome unending open world games graphics, where there is literally no end to the world, are a result of the procedural generation technique.
GTA is a renowned example of this. The sole reason why the game exploded was that the players could do anything, go anywhere, fulfil their wildest fantasies, like jumping a cargo truck from a hill or landing a passenger plane on a fishing boat. It’s crazy, the game offers an unprecedented user experience.
Gamers love open world games, which literally have no end. Run, drive, trek as far as, as long as you can.
It won’t take a genius to figure out that such a massive amount of content cannot be generated manually. It would take years if not decades to launch a game like this.
Such games are only possible via procedural generation. The game code generates the environments on the fly as the player progresses through the game, providing an immersive user experience.
Similarly, this technique is used in movie editing to generate landscapes, fantasy lands, swarms of the crowd in an event, an unending wave of soldiers, wolves running towards the hero just like that in the movies like Lord of the rings & Mummy.
The technique of procedural generation is also used in creating electronic music. Where a few lines of code generates some really innovative music.
Some say, our universe is procedurally generated too ? It keeps on being generated as far as our telescopes see or our spaceships travel. Anyways, that is another super fun topic to discuss, maybe sometime else.
Right now we have to talk about the fundamentals of this stuff.
So, I believe by now you’ve got the gist of what procedural generation really is.
2. Procedural Generation in Game Design
As already discussed above I’ll delve a bit further into how procedural generation is being used in designing games.
2.1 How do I know when to manually create content & when to use procedural generation?
Procedural generation has an element of randomness in it. The algorithm will take into account a set of rules & will randomly generate content for you. On the contrary, when manually creating content, we have a limit on what we can achieve in a stipulated time.
So, ideally, if our game has a certain storyline, & a limited number of levels, we can focus on manually creating the content & putting detail into that for a better user experience as opposed to letting the algorithm create the graphics.
On the other hand, if our game is like an open world, where we encourage the player to go out experience stuff, like go live your life kinds, procedural generation is our best bet.
Remember those 9999999 levels in the 8-bit Tank shooting game? All that stuff was procedurally generated. No way the developers could manually write each & every level.
Also, procedural generation really stands out when there is a limited space, for an instance stuffing a game in an 8-bit chip. Game developers cannot create too many levels manually & put in the chip. That would just eat up too much amount of memory. Hence, they use procedural generation algorithms to generate random maps on the fly when the games are loaded. This technique uses less memory also makes the content less predictable spiking up the fun element.
One of my favourite city building games Kingdom & Castles also offers the users an infinite range of procedurally generated random maps to take their pick from. Every map has a different setting of lakes, trees, natural resources & shores.
Epic stuff. Not that I am associated with them anyway you should definitely checkout that game.
2.2 Use Cases of Procedural Generation in Game Designing
As more & more advanced hardware became available to game developers over time along with the gamers. Game developers used procedural generation to design the availability of loots & rewards in the game. For an instance, if the hero is walking through the enemy turf, different weapons, rewards & mission loots would be made available to the hero at random locations every time through procedural generation.
A couple of more instances where procedural generation really shines in game design.
Based on the user behaviour in the game, the game logic procedurally generates AI characters so that the user can relate more to the character & remains emotionally invested in the game.
Game developers are using procedural generation in designing levels of their game. Based on his performance a player is advanced to custom procedurally generated levels to provide an immersive gaming experience.
Procedural Generation is also being used with Natural Language Processing to create lifelike plots in the games. Generally implemented in Text-Based Games.
2.3 Third Party Procedural Generation Tools & Libraries
There are also several third-party tools and engines/world generators available to the game developers which help in generating content like forests, landscapes and so on. The model of a single pine tree can be used to procedurally generate an entire pine forest. This really speeds up the development process as the developer can now focus on the story than investing time designing landscapes and forests.
Speed Tree is a vegetation modelling and programming software used in cinema and games. It creates real-time simulations of vegetation, forests, foliage, farms, architecture etc. It is used by some of the big guns like Activision, Ubisoft, Epic Games etc.
Gaia is a terrain & scene generation system for Unity 3D. Game developers can leverage the cutting edge algorithms of the system to create beautiful worlds for their games.
3. Procedural Generation Concepts & Algorithms
PCG Procedural Generation can be a complex thing to wrap our head around. It is often closely used in conjunction with AI & deep learning to achieve the desired results.
And things always get super interesting for me when this stuff is used in building games.
I’ll quickly walk us through some of the important concepts & underlying algorithms which would help us understand the techniques of procedural generation.
3.1 Simulating artificial life, Genetic Algorithm
Often in city management games, we observe that along with the user interaction other elements of the game are self-aware & keep running their tasks concurrently with the user input.
This technique is commonly known as simulating artificial life or the Genetic algorithm.
Here is the logic/algorithm:
1. The game starts with an initial population count.
2. The code keeps a check on the health of the population.
3. As the health of the population starts to deteriorate it kills the weak objects & re-generates news off-springs in the game.
This procedural generation technique keeps a check on the population health of the game & keeps generating new people based on the input rules.
3.2 Dungeon Generation – Connect Rooms via Corridors with Spanning Tree
Dungeon generation is an important example of procedural generation. In a dungeon, several rooms are placed on the grid & then tried to connect to each other via corridors.
The layouts of the corridors, connecting the rooms are procedurally generated & with so many combinations and possibilities the end design can get really complex. Higher the complexity, more the fun.
This dungeon generation approach is tested, if the rooms are connected correctly, with the help of a spanning tree.
A similar algorithmic approach is used in city generation. Where the grid layout & the moving vehicles are procedurally generated.
This approach is also more commonly known as the maze generation. Typically used in all the adventure pathfinding games. The maze can be traversed using the depth-first search or breadth-first search algorithms.
3.3 Generating Weather Simulations via Markov Chains
Markov chain is probabilistic modelling. Predicting a future event based on the information of the present event. Markov chains has widespread applications in a plethora of domains such as physics, chemistry, finance, games, genetics, queuing theory etc.
Google search engine’s PageRank algorithm was also based on the Markov Process.
With the help of the Markov chain model, weather simulations are procedurally generated. This way the weather pattern in the game is less predictable, & adds more fun to the player’s experience.
3.4 Generating & propagating fire with the help of Procedural Generation
Procedural generation algorithms are used to generate smoke & fire in games, movies & also used to spread fire in the surrounding environments. Simulating the buildings & houses catch fire & burn.
The algorithm/concept used to achieve this is known as Cellular Automata.
Cellular Automata means in a 2D grid with n number of cells. Each cell will have a certain state & a state of a cell can affect the state of the neighbouring cell based on a certain ruleset. The rules are ideally the same for all the cells in the grid but the state of the individual cells is different.
Cells can change their state concurrently, for an instance a fire in a particular cell can also cause a fire in the neighbouring cells thus changing their state.
3.5 Creating Unique Situations for the Hero in Text-Based Games in conjunction with Natural Language Processing
Text-based games are getting a lot of traction lately especially games integrated with popular messenger apps. Text-based games are games which use text characters to interact with players instead of the vector graphics.
They provide more like a lifelike experience of communication with a real person.
The hero in the game is challenged with unique unprecedented situations generated with the help of procedural generation in conjunction with natural language processing. This stokes up the fun element in the game by notches.
4. How do I use Procedural Generation in My Project?
Now once you are certain you would naturally be well aware of the use case like where you would need the stuff to be procedurally generated. Now is the time to do delve into the specifics & do some digging.
As already stated at the beginning of the article, the procedural generation code can be as simple as generating a series of a number at the same time as complex as designing a series of meshes for the movie Inception.
Just bear in mind the use cases & the concepts, algorithms explained above in the article. It will help you a lot when designing the feature involving procedural generation in your project.
Now get down to coding & ensure regular testing of the code.
4.1 Procedural Generation Resources & Tutorials
Alright, time for some resources on PCG.
If you are writing a game on unity. This unity tutorial might be a good place to start.
Here is a Gamasutra article on a 2D procedural generation tool called Strata. It could be used both by programmers and non-programmers alike. The tool makes use of Scriptable Objects & delegation pattern to generate content. A good read I would say.
Here is another open source procedural generation library for Unity on GitHub.
This was pretty much about starting resources on Unity. If you need to delve into further Google is our best friend.
This freecodecamp article on writing a procedural dungeon map generator using the random walk algorithm is a good read.
Some of the other GitHub repos for inspiration
When stuck with a problem try putting up on your doubts on the Game Development Stack Exchange forum https://gamedev.stackexchange.com/questions/tagged/procedural-generation
Here is a tool for generating random dungeons if you want to try it out.
More on the Blog
This was pretty much it. I hope this article could clear all your concepts about procedural generation.
We are all set. Do let me know in the comments if you liked the article. Any topic or content that is left & should be added.
Any doubts or feedbacks. Do write those down too in the comments. I would love to know about it.
If you enjoyed reading the article, share it with your friends.
Do follow 8bitmen on social media.
See you in the next article!!
- Distributed Systems, Scalability & System Design #1 – Heroku Client Rate Throttling
- Zero to Software/Application Architect – Learning Track
- Java Full Stack Developer – The Complete Roadmap – Part 2 – Let’s Talk
- Java Full Stack Developer – The Complete Roadmap – Part 1 – Let’s Talk
- Best Handpicked Resources To Learn Software Architecture, Distributed Systems & System Design