Making games with Falling Sand part 1

  Рет қаралды 32,689

Winterdev

Winterdev

Күн бұрын

I want to get familiar with the process of releasing a game before I finish Metal Sphere Rising, so I’m planning on making a game in a month, and then releasing it on Steam or something. Then KZfaq started serving me falling sand videos and I thought that it would be cool to use that for this project, and here we are. Let’s look at how the tech behind these simulations work, and then I’ll use them to make a cool game in a few weeks.
Full article: winter.dev/articles/falling-sand
Intro: 0:00
Java version: 1:02
C++ version: 4:25
Adding a player: 6:53
Demo: 7:40

Пікірлер: 83
@Winterdev
@Winterdev 3 жыл бұрын
Minor edit to the code at 6:00, should be m_changes.size() - 1 I believe.
@fnhm_
@fnhm_ 3 жыл бұрын
why are so few views here? it's the best explanation i've ever seen
@prodevus
@prodevus 2 жыл бұрын
I've just browsed through like 4 falling sand simulations on KZfaq and this is by far the best one! You actually explain why it's important to update the cells based on the old pixels rather than the current ones. Also, shuffling the CellMoves at the end of the physics update removes any left-bias present in other sand simulations. My water was falling through my terrain until I implemented the shuffled CellMoves.
@Winterdev
@Winterdev 2 жыл бұрын
Thanks! I don’t know why no one looks at the x bias. Most cover the Y but I haven’t seen one actually make it fully ‘random’. That was one of the main reasons I made this actually haha, Glad it helped!
@prodevus
@prodevus 2 жыл бұрын
@@Winterdev Hey I was watching Screamingsnake’s falling sand video and while he doesn’t explain it in the video, I checked his code and he is using an interesting method you might be interested in, because it skips having to have cellmoves or two buffers. Instead of compiling and shuffling CellMoves, he generates a randomly shuffled array of indexes ranging from 0 to the chunkWidth, and loops through the map or chunk like that row by row. It eliminates having to create a CellMove struct or class and shuffling those afterwards. However it does lead to step stair formations at chunk edges, but those can be resolved by randomly offsetting the thread/chunk matrixes each frame.
@Winterdev
@Winterdev 2 жыл бұрын
Oh that’s interesting so you basically make an iteration map before hand. I was thinking about trying to do this on the GPU to see how fast I could get it to run, that might be the way to do it there. I going to look into that cus I need to use CUDA for a new job so maybe this will be a cool way to train up. Thanks!
@fudgeracoon2529
@fudgeracoon2529 3 жыл бұрын
I gotta say, I'm loving these tutorials. Keep it up man, looking forward for more articles on your website !
@Winterdev
@Winterdev 3 жыл бұрын
Thanks! Next one is going to come out soon. With school starting I am a little delayed though :(
@sanderbos4243
@sanderbos4243 2 жыл бұрын
This is hands down the best explanation of how to code a falling sand game I could find, thanks for taking the time to make all of the visuals!
@sanderbos4243
@sanderbos4243 2 жыл бұрын
5:52 I recommend always iterating through an array/vector in reverse when you want to remove items from it. :-)
@GameEngineSeries
@GameEngineSeries 3 жыл бұрын
Cool! I love your clear explanation and visualizations! Keep up the good work!
@Winterdev
@Winterdev 3 жыл бұрын
Thanks! I wasn't sure this one was gonna be as clear as the others, glad I managed it :)
@zimi992
@zimi992 Жыл бұрын
It is the first example of well defined problems with this kind of physics mechanics. Loved the way you explained them
@Tigrou7777
@Tigrou7777 Жыл бұрын
An alternative approach for handling particles in a efficient way is to maintain 2 collections : one is a 2D array that map a x/y position to a particle (same as what you describe in the video) and the other is a array of particles. Each particle contains a type and a x/y position. It can be a struct to avoid creating many objects. To update simulation, you simply iterate on the particle array. it automatically skip all empty grid cells and prevent you moving a particle more than once.
@DarxDev
@DarxDev 3 жыл бұрын
man you deserve recognition
@Winterdev
@Winterdev 3 жыл бұрын
spread the word ;)
@Vextrove
@Vextrove 2 жыл бұрын
I love this video, please make more videos about "interesting things" like these! :D
@johnjackson9767
@johnjackson9767 3 жыл бұрын
Great vid and explanation, man. I did a video covering a similar topic last year, and I "solved" the iteration issue by swapping which side I began iterating on each frame and then storing whether or not a particular tile has been solved. Double buffering would fix these issues, but that comes with its own set of headaches as well.
@Winterdev
@Winterdev 3 жыл бұрын
Thanks! I watched your vid when researching mine. It's funny how may different ways there are so solve the same problem. I bet the best way to do it is with double buffering + reversing the dir. I was trying to look into running it on the GPU, which I you would have to double buffer for, hinting that's the best way. If I had more time I'd play around with it, but now I'm trying to make a lil game with it, so I called it done.
@johnjackson9767
@johnjackson9767 3 жыл бұрын
@@Winterdev Your solution seems to working just fine, so I say run with it. Looking forward to what you come up with.
@Winterdev
@Winterdev 3 жыл бұрын
@@johnjackson9767 Ye for sure I like messing with the tech too much lol. Should be something cool in the next few weeks or so...
@HylianEvil
@HylianEvil 3 жыл бұрын
Amazing work!
@blumenkohltv1565
@blumenkohltv1565 3 жыл бұрын
Very cool stuff. I expect this channel to grow fast if you can keep up the quality :)
@Winterdev
@Winterdev 3 жыл бұрын
Thanks for the comment, I'm excited to see if it will :)
@thebutterappletutorials6553
@thebutterappletutorials6553 3 жыл бұрын
I would love for the engine to packaged for this to follow along with! I've been playing falling sand games for years and have always wanted to learn more about the physics of them!
@Winterdev
@Winterdev 3 жыл бұрын
Ok I'll start on that it might take a while through
@weltlos
@weltlos 3 жыл бұрын
Good stuff! I am currently working on a game too and will definitely try to implement these things.. at some point. :)
@Winterdev
@Winterdev 3 жыл бұрын
It allows for some cool effects! good luck with your game
@junwooseo785
@junwooseo785 3 жыл бұрын
Nice work!
@Winterdev
@Winterdev 3 жыл бұрын
Thanks!
@qetu10
@qetu10 3 жыл бұрын
Great video! Two suggestions I have are a constant acceleration for gravity, and somehow making water settle faster (e.g. with a some kind of spread factor?) Currently am trying to take inspiration from you but it seems I have to choose between glitchy update-in-place with fast water, or a move queue/double buffering with painfully slow water...
@Winterdev
@Winterdev 3 жыл бұрын
Yeah I was focusing on the engine bits, for making a game with it I am looking at a bunch of different movement properties. I got this i.gyazo.com/3760bbd2b0d41794376463aed0cab5ba.mp4 by checking 3, then 2, then 1 away for water. I think you would do that with a velocity and acceleration for gravity.
@qetu10
@qetu10 3 жыл бұрын
@@Winterdev that looks better yeah! have you thought about implementing a fluid simulation alongside with something like FLIP or SPH? Something that bothered me about these falling sand games is that the water never looks realistic... that might be a me problem though
@Winterdev
@Winterdev 3 жыл бұрын
@@qetu10 That would be really cool. I am going to look into that haha! You just run a set of eqs. right? I bet that wouldn't actually be too hard with the fixed grid, I'll see. It would be really cool to see a splash/pressure in a pipe.
@qetu10
@qetu10 3 жыл бұрын
@@Winterdev good luck with your research, will be better than my attempt in any case - the equations all go over my head lol
@matthewmathis62
@matthewmathis62 Жыл бұрын
Hey man, I just noticed that your Description is not Algorithm Optimized. The first few words in your Description are used by KZfaq to help people find your video (KZfaq says this in the helper text next to the input box) So, I think it should have keywords and be descriptive of your video, such as: "How to Make a Falling Sand Game in Unity.". Something like that. Just thought I'd tell you!
@chakibchemso
@chakibchemso 11 ай бұрын
This can benefit so much from ECS
@chakibchemso
@chakibchemso 11 ай бұрын
Or cuda depending on the algorithm and data structure
@Bonfere
@Bonfere 3 жыл бұрын
nice video
@YoTengoUnLCD
@YoTengoUnLCD 3 жыл бұрын
The “issue” with iteration seems completely artificial, just use a new array and double-buffer the particles.
@Winterdev
@Winterdev 3 жыл бұрын
That's another way to do it, and what I did first, but to get real independent ordering you need to consider all the possibilities before applying them. It's a little less clear than I would like but what you suggest is how the Processing version works, and the C++ version is just another way that truly doesn't have any bias to the order.
@player210
@player210 3 жыл бұрын
this method takes a lot of cpu's power
@Winterdev
@Winterdev 3 жыл бұрын
@@player210 Yea I wish that sort didn't need to happen, in the next part I am going to look at the performance, as I am going to expand the size of the world
@player210
@player210 3 жыл бұрын
@@Winterdev i'm really excited about second part!
@player210
@player210 3 жыл бұрын
please, do part two!
@Winterdev
@Winterdev 3 жыл бұрын
Coming soon! :)
@RaniLink
@RaniLink Жыл бұрын
Did I miss the solution for the difference in direction or there wasn't one in the vid? This is more evident when you add velocity to the particles
@snarkbucham4077
@snarkbucham4077 2 жыл бұрын
Hello, very good video, thank you very much, even so, I have a doubt. In c ++, when you put the new functions in the sandworld class, the constructor and destructor change. What happened to these? Why lose the {} and you put ";" at the end? And where does the delete[] function go? It is still the declaration of the class right? Or is it the calls to constructors and destructors? and in that case did you put them out of class or where? The truth is that there I have totally lost the thread.
@Winterdev
@Winterdev 2 жыл бұрын
That was a little confusing but I was just trying to compact it away. In c++ you can declare a function before implementing the body so it’s implied that it gets moved. But I don’t think I ever said that I was just trying to make room for the next code.
@snarkbucham4077
@snarkbucham4077 2 жыл бұрын
@@Winterdev I see, thank you very much for your quick response. Great work. All the best.
@SamudrarajOfficial
@SamudrarajOfficial Жыл бұрын
@@Winterdev can u tell me which java scripting application u were using?
@void6048
@void6048 2 жыл бұрын
Am gonna make a gaem… dat pretty much simulates teh universe… u can put gravity stuff dat creates blacc holes and make planets dat eventually build up ranging from just molecules to building an entire sun it gonna be so cool xdddd
@lorddonqweetle
@lorddonqweetle 3 жыл бұрын
how do you play it after you code it? i want to use the water and sand for a game i am making but cant code at all :D
@lorddonqweetle
@lorddonqweetle 3 жыл бұрын
also there were many mistakes that where red in the code how do i fix those?
@Winterdev
@Winterdev 3 жыл бұрын
Thanks for the comment! There are some engine pieces that I didn't include for the c++ version. You'd need to be able to run some graphics library that lets you draw pixels to a texture. That could get a little complex if you 'cant code at all', so I'd start with the processing version as all the tools are built in to the program. Here's the download processing.org/download/. If you write the java code from the video it will work. You can also copy it from the article blog.winter.dev/2020/falling-sand-games/. Good luck :)
@lorddonqweetle
@lorddonqweetle 3 жыл бұрын
@@Winterdev ok thx
@drollestprawn
@drollestprawn 3 жыл бұрын
Great video. What would be the rules if it were liquid?
@Winterdev
@Winterdev 3 жыл бұрын
Thanks! Do you mean water cells? The rules are at 2:32
@drollestprawn
@drollestprawn 3 жыл бұрын
​@@Winterdev yes. so if it can go down it will but if it cant then where would it move? is it exactly like sand but to the left and right?
@Winterdev
@Winterdev 3 жыл бұрын
@@drollestprawn Yes, you would just not add 1 to the Y. So (x, y + 1), (x - 1, y), (x + 1, y)
@drollestprawn
@drollestprawn 3 жыл бұрын
@@Winterdev ok thanks
@Fezezen
@Fezezen 2 жыл бұрын
I cannot access your website by the way. Prompts a "Your connection is not private" warning on like every browser I try
@Winterdev
@Winterdev 2 жыл бұрын
Thanks for noting this, It’s fixed! been super busy hadn’t looked at it
@jareddixon5811
@jareddixon5811 2 жыл бұрын
strange I'm using processing 4(beta) and it says: The function "setCell(int, int, int)" does not exist. any idea whats the problem?
@jareddixon5811
@jareddixon5811 2 жыл бұрын
same for the function "isEmpty"
@Winterdev
@Winterdev 2 жыл бұрын
Hmm either they are not defined or maybe try putting them above the loops? They are my functions btw not included in processing
@onfox1864
@onfox1864 2 жыл бұрын
try just "set"
@Genamae564
@Genamae564 Жыл бұрын
How do I import pictures into the Falling sand
@Winterdev
@Winterdev Жыл бұрын
You can use this library github.com/nothings/stb/blob/master/stb_image.h and paste it in like the tiles. If you want to rotate it tho you need to render it with a software renderer into the sand world or a regular sprite on top of the sand
@MagisterStone
@MagisterStone 2 жыл бұрын
What ide do u use for such short code in Java?
@Winterdev
@Winterdev 2 жыл бұрын
It’s this thing called processing, it turns Java into a kind of scripting lang almost. It comes with an ide called processing? It’s not so good but has some interesting features like you can edit variables live with sliders
@MagisterStone
@MagisterStone 2 жыл бұрын
@@Winterdev I'm trying to write the same on cpp, using direct2d gpu but in this case I need to write a lot of stuff like wndclass and window creation, wndproc, handling messages, implementation of devices for direct2d. That's interesting, but I whant to concentrate on code and when I see such videos as yours I am asking myself how to do that so easily? Also there are questions about solving particles movements, i meen how to render them with high fps in case its more then 1000 of them. I have tried to make quadtree and that helped a bit, but I sow videos with thousands particles collided and rendered with high fps. Where should I store them: lists or massive?
@MisterFanwank
@MisterFanwank 2 жыл бұрын
Jesus Christ, just use two buffers and swap which one you use each frame.
@Serotonindude
@Serotonindude 2 жыл бұрын
too fast, aprat from that, great!
@lourdthebluefoxie
@lourdthebluefoxie 3 жыл бұрын
Bruh I swear i commented but it's not appearing?
@Winterdev
@Winterdev 3 жыл бұрын
I see no comment held for review or whatever so idk, what were you trying to say?
@lourdthebluefoxie
@lourdthebluefoxie 3 жыл бұрын
@@Winterdev in saying that i commented in your video before this but is gone
@Winterdev
@Winterdev 3 жыл бұрын
@@lourdthebluefoxie yeah what was the comment? maybe it'll work as a reply?
@__8120
@__8120 2 жыл бұрын
Anyone else severely triggered by the indenting lmao
@theitatit
@theitatit 2 жыл бұрын
what do you not like? Code is very readable.
@__8120
@__8120 2 жыл бұрын
@@theitatit mainly all the extreme amounts of excess spacing to make the things line up, like the if statements at 2:17 are a perfect example
Making an infinite world with Falling Sand part 2
10:07
Winterdev
Рет қаралды 15 М.
🦕 Top 9 Ways To Make Big Sand
9:17
TodePond
Рет қаралды 32 М.
50 YouTubers Fight For $1,000,000
41:27
MrBeast
Рет қаралды 158 МЛН
Can You Draw A PERFECTLY Dotted Circle?
00:55
Stokes Twins
Рет қаралды 50 МЛН
Этот Пёс Кое-Что Наделал 😳
00:31
Глеб Рандалайнен
Рет қаралды 6 МЛН
Recreating Noita's Sand Simulation in C and OpenGL | Game Engineering
10:03
Noita Gameplay - Explaining what every pixel is simulated means
10:28
💧 Top 9 Ways To Make Water
6:36
TodePond
Рет қаралды 43 М.
Designing a Physics Engine in 5 minutes
7:37
Winterdev
Рет қаралды 150 М.
Making My Own Programming Language and Coding a Game in It
10:19
AstroSam
Рет қаралды 1,2 МЛН
A new way to generate worlds (stitched WFC)
10:51
Watt Designs
Рет қаралды 517 М.
Using AI to Create the Perfect Keyboard
12:05
adumb
Рет қаралды 1,4 МЛН
Simulating the Evolution of Rock, Paper, Scissors
15:00
Primer
Рет қаралды 1 МЛН
Simulating Particle Life
18:18
Digital Genius
Рет қаралды 174 М.
Exploring the Tech and Design of Noita
31:00
GDC
Рет қаралды 221 М.
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 7 МЛН
S24 Ultra and IPhone 14 Pro Max telephoto shooting comparison #shorts
0:15
Photographer Army
Рет қаралды 7 МЛН