Page 1 of 1

Very simple falling sand simulation

Posted: Fri Sep 29, 2023 7:37 pm
by whateverest
A few weeks back I stumbled across one of this falling sand simulations on the web and wondered how hard it would be to implement this.
Turns out it is very simple, the rules for the falling sand for example are implemented with only 10 lines!

Writing it was really fun - I haven't used löve/lua for years. Playing around with it is surprisingly fun too so I decided to share it here.
screenshot.png
screenshot.png (17.49 KiB) Viewed 18403 times
Considering that I did not spend a lot of time on it I am pretty happy with the result.
The only type of cell that I am not quite happy with is the water. Sometimes it behaves kinda weird and the implementation seems unnecessarily complex.

7 Types of cells are implemented. You can select them by using the number keys and you can "draw" the cells by left clicking.
  1. Sand
  2. Water
  3. (Floating ;)) Concrete
  4. Plants - Grows to nearby cells until a certain height is reached
  5. Eraser - Deletes cells
  6. Fire - Spreads to and consumes nearby plants. Spawns Smoke Above itself
The code is not object oriented, which fit this project quite well i think. Currently the field has only 200 x 130 cells, but I think the simulation should be able to handle much larger areas.

The source code can be found here: https://codeberg.org/ycake/sandsimulation.

Re: Very simple falling sand simulation

Posted: Fri Sep 29, 2023 10:03 pm
by dusoft
Nice sandbox!

Re: Very simple falling sand simulation

Posted: Fri Sep 29, 2023 11:03 pm
by togFox
This is really cool. You should find a way to export the result and then make it a side scrolling map like Terrairia or something.

"Jake - look at this - I sprinkled all this stuff everywhere then exported and started jumping around it and explored this thing that is mine. I'm going to do that again and see how whacky I can make it. "

8-)

Re: Very simple falling sand simulation

Posted: Sat Sep 30, 2023 5:37 pm
by knorke
Nice project.
Small problem, the window does not fully fit on my laptop's screen.

Re: Very simple falling sand simulation

Posted: Sat Sep 30, 2023 10:25 pm
by BrotSagtMist
But o gosh the cpu usage :D
I actually love this kind of simulations, now ad stuff that acts automous, cows that eat the grass, fish that swim in the water, b2 bombers that drop poop bombs from the sky.

Re: Very simple falling sand simulation

Posted: Sun Oct 01, 2023 5:41 pm
by whateverest
knorke wrote: Sat Sep 30, 2023 5:37 pm Nice project.
Small problem, the window does not fully fit on my laptop's screen.
Thanks!
The Window should be resizable but that obviously does not help too much if the simulation area does not fit on the screen anyway.
Maybe I get around to adding a very basic zoom function sometime.
BrotSagtMist wrote: Sat Sep 30, 2023 10:25 pm But o gosh the cpu usage :D
I actually love this kind of simulations, now ad stuff that acts automous, cows that eat the grass, fish that swim in the water, b2 bombers that drop poop bombs from the sky.
Runs fine on my machine :crazy: But true, performance optimization is not something I too much thought into and I probably won't for this small fun-project :).

Yeah I love how much cool behavior you can get with very simple rules. You could probably add endless amount of new cell types.

Re: Very simple falling sand simulation

Posted: Sun Oct 01, 2023 6:41 pm
by GVovkiv
whateverest wrote: Sun Oct 01, 2023 5:41 pm
Thanks!
The Window should be resizable but that obviously does not help too much if the simulation area does not fit on the screen anyway.
Maybe I get around to adding a very basic zoom function sometime.
There, in fact, several libraries that could scale game content to any window size, which might help with this task.
This libraries in semi-active to barely active states
https://github.com/Vovkiv/resolution_solution
https://github.com/Ulydev/push
https://github.com/S-Walrus/center

While this libraries is abandoned
https://github.com/adekto/maid64
https://github.com/tomlum/simpleScale

Re: Very simple falling sand simulation

Posted: Mon Oct 02, 2023 1:12 am
by togFox

Re: Very simple falling sand simulation

Posted: Fri Oct 13, 2023 10:25 pm
by whateverest
Thanks for the suggestions!

In the end i went with a very simple zoom function using the mouse wheel though. That should make the program at least usable on smaller screens.
The last thing I would like to add, before I'll probably abandon the project, would be to make the simulation size configurable so it doesn't melt peoples PCs :) .

Re: Very simple falling sand simulation

Posted: Fri Oct 13, 2023 10:32 pm
by BrotSagtMist
Havnt looked at your code but from own simulations i found skipping works really well.
Eg work only even objects one frame and uneven the next, behaves almost the same but instant halves cpu usage.