Recreating Noita's Sand Simulation in C and OpenGL | Game Engineering

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey, everyone. Noita's my favorite game at the moment, so here's my most recent post discussing some of the simulation techniques it uses as well as show off how I recreated them with C and OpenGL.

Edit: Thanks for the Gold Award, to whomever gifted that. Incredibly humbled by that.

👍︎︎ 11 👤︎︎ u/misterfrenik 📅︎︎ Apr 24 2020 🗫︎ replies

You are great at explaining. Visually nice. Amazing.

👍︎︎ 4 👤︎︎ u/[deleted] 📅︎︎ Apr 24 2020 🗫︎ replies

Reminds me of the game Liero, not to mention Worms 1 :-)

👍︎︎ 2 👤︎︎ u/Madsy9 📅︎︎ Apr 24 2020 🗫︎ replies

Do you have a compiled version somewhere?

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Apr 24 2020 🗫︎ replies

You have some great ideas in that video, and they're actually very simple to implement. It kind of makes me want to write a system like this. Are you doing all of the particle simulation on the CPU or the GPU? Thanks for sharing!

👍︎︎ 1 👤︎︎ u/fgennari 📅︎︎ Apr 24 2020 🗫︎ replies

Great video! It was really straight-forward and easy to understand for me

👍︎︎ 1 👤︎︎ u/Mystonic 📅︎︎ Apr 24 2020 🗫︎ replies

Great job! Looking forward to checking out your sourcecode.

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Apr 24 2020 🗫︎ replies

Cross post this to r/howdidtheycodeit

👍︎︎ 1 👤︎︎ u/SimDeBeau 📅︎︎ Apr 24 2020 🗫︎ replies
Captions
lately I've been playing a lot of Noida the most recent release from NOLA games it combines my favorite genre roguelikes with the mechanics of popular falling sand games like powder toy while playing I realized that I'd never taken the time to dive in and figure out how these types of sand games work so with that in mind let's explore the tech behind sand games like Noida as I discuss how I implemented my version of it [Music] for this I'm using gunslinger which is my open-source c99 framework I've been working on for games tools and visualizers it's portable and compiles for Windows Mac and Linux and it comes with a few examples for quickly getting started I even used it to make all the animations for this video using a really crappy vector renderer I wrote over the course of a few days there's a github link in the description for anyone interested in checking it out in my research I was able to find multiple websites as well as a GDC talk were one of the devs from NOLA games Petri discusses a lot of the algorithms surrounding how a simple sand falling game works as well as how the data could be structured and processed first we need to define a few things falling sand simulations are basically a form of complex cellular automata you can think of most cellular automata zazz a type of turn-based game where you have a world that's comprised of a grid of cells and where each turn you modify the state of each cell in your grid based on the neighbors that surround that cell one of the most classically used examples is John Conway's Game of Life as you can tell these rules are really simple but incredibly complex machines and simulations can be built out of just using these rules alright so with that said let's go back to Noida our entire game world is made up of a 2d array of particle data each particle is the structure containing all of the information it needs in order to know how to behave in the world such as its ID total lifetime color and whether or not it's been updated yet for this particular frame each frame of our simulation we iterate through the particle data row by column for each cell and our grid we examine its ID type for empty cells we ignore them and for any other IDs we'll call the update function for that cells type so let's take a look at a contrived example here we have a single sand block placed somewhere in the grid when we get to its update loop the sand will follow this set of rules if the block below it is empty we'll move down otherwise if the block below into the left is empty we'll move down to the left otherwise if the block below and to the right is empty then we'll move there and if all else fails we'll just stay put and with just this the results are quite interesting already and the beauty of this is that the rules are really simple but we can alter them and change certain properties in order to get more refined and realistic configurations as we'll see later on I should also mention as a general rule all of our particles will be confined to the space of the world and simply won't be able to leave the bounds of it [Music] water and other liquids initially behave exactly the same as sand falling down and looking left and right below them for a place to move however unlike sand when these first three rules fail they will also look to their immediate left and right respectively this tends to give off the effect of flowing to simulate the property that water will attempt to fill any void it finds available to it so again with these simple rules we have a decent starting point for sand and water but we're not done as I mentioned earlier these rules can be modified and adapted in order to achieve more realistic results and up until this point we haven't actually been taking velocity or any other forces into account this of course means that all of our particles will fall at a constant speed of one cell per frame and the water will flow well just as slowly let's add a constant rate of acceleration for gravity to change our velocity as well as for liquids a spread factor velocity which will allow us to spread faster left and right yeah that looks terrible here's the issue previously each particle could only ever move one cell at a time so it was only ever interested in its immediate neighbors when we had velocity or any other forces into the mix we're now interested in every single particle that our particle could interact with along its way to its final position so looking at this example the water there are moments where the water is unable to spread anywhere because it's requested position is unavailable so it stops completely and causes this repeated jagged effect so here's my naive solution to fix this using the particles velocity will compute its desired next position for this frame well then travel to every cell along that path if we run into anything along the way as is the case here we'll stop and just leave the particle in the last free spot that we found now with that change our water is able to flow in a much more convincing manner and our sand particles can fall at a faster rate all right time to add in another particle let's do something stationary made out of wood and this thing's pretty simple to update it won't be affected by gravity or any other forces however whenever the particles start to fall you start to get some really cool emergent behaviour especially when you say erase parts of the wall or when you flood various valleys and areas and watch it spill over into the surrounding floor I want to jump ahead to fire it's one of my favorite particles mainly because of all the dynamic behaviors that just naturally emerge from it its destructive it only lives for a short amount of time and in its most simple version the only way it's able to move is by consuming particles next to it that are flammable this also generates a lot of smoke as you can see from the footage smoke is pretty simple to do if you think about it it's really just sand but inverted so instead of falling down due to gravity the smoke will just rise to the top of the world and then kind of pool up there so at this point we've assembled kind of a nice collection of particles we have sand water fire embers and smoke adding new ones really should become second nature once we have our base rules down gunpowder for instance works almost exactly the same as sand does however it has a very high flammability chance salts works exactly the same as sand as well however it has a chance to dissolve in water it's also less dense than water so you can say that if it's surrounded by water particles for instance then it has a chance to rise to the top lava is liquid in movement however it has a really high chance to catch things on fire steam works just like smoke however it's a byproduct of whenever fire and water touch oil works exactly the same as water and movement except it's very flammable and acid works exactly the same in movement as any other liquid but it's very highly corrosive and will eat through most things so I think we have our basic implementation down but as a final polish let's go ahead and make this feel like an actual tool where you can select whatever particle you want to paint with on the side visually everything's pretty underwhelming so let's add in a bit of post-processing like some bloom and a little bit of gamma correction yeah that's feeling better but there's still something not right to me see most cellular automata is you can paint in little States but typically you start with a starting state and we're not really doing that here so here's what I want to do let's allow the user to drag and drop images from disk into the program then we'll take all the pixel data from that image and we'll create particles for each one so that we can interact with the scene now you'll notice most images are rich in color they're typically 32-bit rgba and we have a very limited color palette with just our particles so what we'll have to do is very simply map this rich 32-bit RGB a data down to our small limited palette and this is easy enough to do we'll just look at every single pixel and we'll match that with our pallet range and based on at least Euclidean distance algorithm we'll select the most appropriate one of course there's going to be artifacts with this but it's simple enough for this application and I really think it has a certain sense of charm to it [Music] so that's it for the video I hope you found this interesting all the source code for this project is available online open source I'll have a github link down in the description I'm also gonna put a link to my discord channel so if you have any questions about games graphics or programming in general feel free to jump on there and ask me or any one of the other devs that's on there and I'm kind of treating this as a launchpad for a new series where I'd like to look at mechanics especially from popular video games and dissect those and kind of do my own implementation so if you have any suggestions for what you'd like to see please let me know in the comments or in the discord channel I'd love to hear from you anyway thanks for watching and I hope to see you in the next one you
Info
Channel: John Jackson
Views: 155,635
Rating: 4.9856939 out of 5
Keywords: C++, OpenGL, Game, Engine, Games, 2D, Programming, RogueLike, Game Engine, C Programming, Noita, How To, From Scratch, Sand Game, Tech, Game Dev, Education
Id: VLZjd_Y1gJ8
Channel Id: undefined
Length: 10min 3sec (603 seconds)
Published: Thu Apr 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.