You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

3.0 KiB

title: A Simple Falling Sand Simulation date: 07-12-2020 Having spent considerable time away from C++ due to university and general busyness I decided it was time to have another go at learning it, and promptly discovered I'd forgotten everything I'd ever known about initializers, RAII and OOP. {=html} <video autoplay="" loop="" width="100%"> <source src="https://alistairmichael.com/media/particles.mp4" type="video/mp4"> </video> I started my holiday thinking about writing some simple graphic user interface app, and after wasting too much time trying to find a UI toolkit that I could tolerate learning for fun (wx, gtk, and qt are all, unsurprisingly, not simple) I stumbled upon this nice video about writing a simple sand physics game and thought it would be much more fun to work on something conceptually simple and satisfying, compensate for C++'s induced headaches. Inspired by the wonderful work Nolla games did on Noita's sand physics engine I set to work with a SDL2 texture and array of Uint32 pixels. The basic idea is wonderfully simple: its a cellular automaton where you update each particle each frame, according to a simple set of rules. This gets complicated when you want to say, swap the position of two particles so that less dense particles float up through the more dense ones, but it worked out okay in the end. After about 5 hours of fighting C++ I was wishing I'd written it in C. Oh, it it would be so much simpler if everything was just arrays: the grid an array of (int)enum Particle;'s, the update functions an array of function pointers: void (update*)(enum Particle**, int x, int y), the particles an array of pointers to struct's with state and a function pointer: I could change a particle's type simply by rewriting the update function pointer and the Particle's type flag. Who needs inheritance? I could cleanly separate the physics state and Particle properties from my logic and define the interaction between particles purely with their data: I could even read it from a text file!. Anyway I eventually figured out how to inherit initializers and destructors correctly. Particle behaviour can be defined via inheritance relations, and the colour palettes, default physics values, et cetera, are still separated from the actual class definitions so I can easily serialise them to and from the disk if I want to implement that some time (I probably won't). I do really enjoy having RAII and overloading. Perhaps most, I appreciate the greater expressiveness of C++ code over C, where I often find myself off yak-shaving something to do with char * arrays. At the moment I think my goal is to just write enough C++ that most of the time I can spot my stupid mistakes so I can use it instead of C or Java without it being a huge pain. Maybe that is too ambitious. At any rate, its probably the best option for working on games, and I like pointers too much now.