Falling sand simulation: How fast can it go?
Вставка
- Опубліковано 7 вер 2024
- A falling sand simulation based on a cellular automaton made in C++ with SDL and OpenGL.
I recently saw this video • Recreating Noita's San... by John Jackson on the physics engine behind the game Noita. I decided to build my own in C++ with OpenGL.
The simulation is based on a cellular automaton and simulates different materials and their interactions. The program supports different grid resolution. This video shows a 480x270 grid running at 240 ticks per second. It can save and load the grid state as an image for edition in an external image editor. I tried to improve the visuals with the help of a ray marched fragment shader. How fast can it go? An update tick on that resolution takes less than 2ms on my CPU. That's about 500 ticks per second. Pretty fast!
This is still a naive approach, doing most of the work on the CPU. An even faster approach would be to calculate the physics part in a compute shader and do all the work on the GPU).
Thanks for watching!
Update 11/2022
I have no plans on putting this out on itch.io or github, but since several people asked about details of the implementation or if you'd like to try it out, you can find a windows build and the sources here: www.dropbox.co...
Be warned though, this is neither well documented nor well implemented. There's no UI and it might not work on your system. Check the Controls.txt file in the folder for how to interact with it.
If you wanted to play a sand simulation, you shoulda looked up "Powder Game"
Check out noita its an entire world based on falling sand simulations
was just about to make a powder game joke lol
Nice... Looks super cool.. Make more videos please.. I liked your vids.. They are so creative and awesome...
why so many .s?
@@alsire1 Oopssie.. I didn't noticsee..
Great video! I love Cellular automata simulations :) Btw there is a good video about Noita's sand simulations by Jhon Jackson.
Thanks! Yes, I'm pretty sure that's the video that got me started on this.
Why does everyone spell John incorrectly?
I like it!
I think you should add some Clouds and Rain Droplets so it feels like a sim of life! 😊
Very nice
Incredible!
Looks great! I'm making a falling sand simulator too, though yours is much more advanced. Do you also make moving particles interact with other moving particles? Or do they only interact with particles that have stopped moving? I did the first case, and I ran into the problem of every other particle falling one to the right/left because they detected the particle beneath them.
Thanks for your comment! I don't really have moving particles. My grid always has the same number of particles (the cells that are empty have a "nothing" particle in them). I then just go over the grid, cell by cell during each frame and swap particles around. I mark every particle with the frame it got updated, so if a particle A swaps its position with a particle B, A is marked. If later in the frame I get to the cell that now holds A, I know I already moved it so I just skip it. Since B moved to the position of A which has just been handled, it won't get updated during this frame either.
@@zeprodiliuswackelzahn558 I see, that's a much better approach than I took to updating the particles. Thanks for explaining this, I'll definitely be able to improve my simulation with your method!
Search up the "powder toy".
Yeee
hey I've been trying to make a falling sand in C++ too. Any tips for optimization? My game seems to slow down a lot because of the pathfinding of the particles.
Hey, thanks for watching!
I'd say most important is to check whether your approach and your algorithms can be simplified/otimized, maybe sacrificing quality for speed. Then I'd profile and see if I can find some low-hanging fruits.
My approach is very basic. I don't have any pathfinding and the "particles" are just tiny structs that track material(or "nothing"), age and what frame they've seen last. My update loop is a big switch-case that checks the neighbors of each untouched particle against a few hard-coded rules and if a particle moves, I swap it with its neighbor. It is a low quality simulation but its fast.
My helper functions in the update loop are either macros or inlined and I made sure all checks fail early. I use the most aggressive compiler optimization (/O2) and I'm on a pretty fast CPU to begin with.
Best of luck with your project, looking forward to seeing a demo on your channel some day!
Wow thats so fast. Is this multithreaded or something?
Thanks! The grid is updated in a single thread although that's seperate from the main/drawing thread.
Hi man! looks awesome!
How did you solve the fact that iteration direction (right\left) affects the behavior of the cells?
I randomize the direction every loop with a very cheap pseudo-rng. I did adapt the order in which the rules are checked if something looked funky, but that mainly applies to the up/down direction which is deterministic.
@@zeprodiliuswackelzahn558 thanks for the quick reply!
Great! 😃
Very useful!
What were the rules for the water? It looks amazing!
You can see for yourself in the code (linked in the video description), line 268 in Sandgrid.cpp
Hey man, amazing video, I just wanted to know is how you draw the pixels in openGL? I'm pretty new at it and currently learning, I would love to learn how to draw single pixels and do some other experiments like falling sand and boids, thanks
Hey, thanks for watching! The grid data is directly stored into a texture (a 2D color array). The OpenGL part just draws a quad (in orthographic projection) and the fragment shader then samples the texture and displays the color (or in my case, interprets the color and does some additional stuff like changing the colors of the fire material).
Which are the laws governing the sand flow?
Check out the video by John Jackson linked in the description, my sand follows the same logic: Check if the sand can move to the cell below, otherwise check if the sand can move to the cells left and right of the cell below the sand. For the other materials, you can take a look at the source code, also linked in the description.
Where can I get this?
I've added a download link to the description, but it's a very rough prototype. I built this for the sake of building so there's no UI, etc.