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.

КОМЕНТАРІ • 34

  • @snaek2594
    @snaek2594 Рік тому +9

    If you wanted to play a sand simulation, you shoulda looked up "Powder Game"

    • @danibiyarslanov
      @danibiyarslanov Рік тому

      Check out noita its an entire world based on falling sand simulations

    • @GroupNebula563
      @GroupNebula563 Рік тому

      was just about to make a powder game joke lol

  • @stevesajeev6477
    @stevesajeev6477 3 роки тому +8

    Nice... Looks super cool.. Make more videos please.. I liked your vids.. They are so creative and awesome...

    • @alsire1
      @alsire1 Рік тому

      why so many .s?

    • @stevesajeev6477
      @stevesajeev6477 4 місяці тому

      @@alsire1 Oopssie.. I didn't noticsee..

  • @condescendingsnail3722
    @condescendingsnail3722 2 роки тому +4

    Great video! I love Cellular automata simulations :) Btw there is a good video about Noita's sand simulations by Jhon Jackson.

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  2 роки тому +2

      Thanks! Yes, I'm pretty sure that's the video that got me started on this.

    • @gigabit6226
      @gigabit6226 2 роки тому +2

      Why does everyone spell John incorrectly?

  • @matthewmathis62
    @matthewmathis62 Рік тому +1

    I like it!
    I think you should add some Clouds and Rain Droplets so it feels like a sim of life! 😊

  • @johnjackson9767
    @johnjackson9767 2 роки тому +1

    Very nice

  • @LogicEu
    @LogicEu 2 роки тому

    Incredible!

  • @willem6161
    @willem6161 Рік тому +1

    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.

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  Рік тому +1

      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.

    • @willem6161
      @willem6161 Рік тому +1

      ​@@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!

  • @kreeeprrthehut6327
    @kreeeprrthehut6327 Рік тому +1

    Search up the "powder toy".

  • @braver210
    @braver210 2 роки тому +2

    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.

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  2 роки тому +3

      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!

  • @drollestprawn
    @drollestprawn 2 роки тому +1

    Wow thats so fast. Is this multithreaded or something?

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  2 роки тому +1

      Thanks! The grid is updated in a single thread although that's seperate from the main/drawing thread.

  • @RaniLink
    @RaniLink Рік тому

    Hi man! looks awesome!
    How did you solve the fact that iteration direction (right\left) affects the behavior of the cells?

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  Рік тому +1

      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.

    • @RaniLink
      @RaniLink Рік тому

      @@zeprodiliuswackelzahn558 thanks for the quick reply!

  • @samarthtandale9121
    @samarthtandale9121 2 роки тому

    Great! 😃

  • @stupidhoirse
    @stupidhoirse 8 місяців тому

    Very useful!

  • @wilsh1630
    @wilsh1630 Рік тому

    What were the rules for the water? It looks amazing!

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  Рік тому

      You can see for yourself in the code (linked in the video description), line 268 in Sandgrid.cpp

  • @technoo4891
    @technoo4891 3 роки тому +3

    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

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  3 роки тому +4

      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).

  • @michelebrun613
    @michelebrun613 Рік тому

    Which are the laws governing the sand flow?

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  Рік тому

      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.

  • @lNGSOC
    @lNGSOC Рік тому

    Where can I get this?

    • @zeprodiliuswackelzahn558
      @zeprodiliuswackelzahn558  Рік тому

      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.