Update #3 On Project Mole Munch

This week we finally managed to get the water flowing without lagging up the entire computer. To give a short recap from the concept (more can be found in the first post here), I’ll describe the purpose of the water as an artifact in the game. Further down in the post I’ll describe how it was achieved and the motivation behind the steps taken to make it work.

First off, the water is the main threat to the player. Especially underground, since that’s where the water drowns the player resulting in losing the level.  GardenerWater

If the Gardener spots one of the holes made by the Mole, he fills it with water starting a chain reaction.

water

This is how it looks under ground. The water fills with a slow and steady pace at the moment but the speed will be adjustable in later updates of the game.

Now to how this works. Since I don’t want to be overly technical and exclude people with no programming experience, I won’t post long copies of the code since that’s not really interesting to look at. But instead I’ll try to explain more generally what I did.

The first thing that happens, is when the Gardener collides with a hole, he ”plants a seed” that then starts to grow independently. With ”plant a seed” I mean a function is called that draws a pixel sized rectangle shape with a blue color at that exact position but placed under ground.

The second thing is a Update function in the level that every frame checks the level’s texture for this specific blue color. If any pixel is found with that exact RGB code it starts the chain reaction.

Thirdly this chain reaction uses a simple algorithm for checking every blue pixel’s neighboring pixels known as flood fill. Here we also specify the algorithm to only make neighboring pixels blue if they are fully transparent. Thus only filling up the tunnels and not the dirt surroundings.

Now since this an extremely expensive operation, checking every pixel, we used the graphic card’s shader program. To give you an idea of how powerful this is: if you have a resolution of 1920×1080. That’s over two million pixels… Checked every frame… With our in game 30 fps, it’s over 62 million pixels checked every second!

When we previously tried using flood fill, using SFML’s image class, it only worked for a couple of seconds before the game completely froze. Now we do it without any lag at all. We will optimize it even further later on by cutting the resolution of the level’s texture in half.

One effect of always checking every pixel is that you get a nice spread of the water if you dig a new tunnel into a water filled one.water filling

This is as mentioned earlier a general overview since shaders and buffer texture are difficult to understand if you haven’t tried OpenGL and the GLSL language used. But hopefully you’ll enjoy the post anyway and if you are familiar with programming and the concepts in this paragraph, it hopefully provided some insight.

And special thanks to Simon Wulf for suggesting the solution and showing how shaders work!

If you really wanna get into how this works ”code wise”, here is a blog post worth looking at.

About Jens Berg

2014  Programming