So I’ve got some code I found, built in JS that works in a browser (it uses webgl for display, but that’s removable, and it uses some JS features we don’t have that I’ll have to work around (like array push)), but the results? That looks right. And of course, I’ve done the research into what the physics of a lava lamp is, and how it works. And also of course, the code that “looks right” isn’t actually “right” and doesn’t really do a lava lamp ‘right’, but then it looks right… except now that I look closely, all I can see is the wrong. Sigh.
I also found some code that should be able to do it the right way, but of course, will it look right, and run fast enough?
The ‘looks right, does it wrong’ code: https://scruffynerf.github.io/lava-lamp/
Code that should do it, but who knows: Fluid Dynamics Demo
Yes, I could do metaballs, and fake it all. But what’s the fun in that?
I learned, among other things, that Cloudflare uses Lavalamps in it’s network.
The right way: you need to track the temperature of the ‘wax’ particles, so that their density rises and falls above and below 1 (the density of water). The cheating code looks at the temperature of where the particle is, and ‘bumps it faster’, but then there is no heat exchange between particles (which is what happens in real life). Yes, motion is energy, as is temperature (also energy), but the end result look incorrect, despite how much it looks right in so many other ways. You could even ‘cheat’, and just alter density directly from the temperature, so long as in some way, you do things like having particles equalize density (if you push two together, they should do that, not bounce off each other.). Density is what makes it rise/fall, relative to the water. Never mind surface tension, and more factors unthought of so far.
And if all of the ‘lava lamp’ code for LEDs out there cheats, then doing it the right way seems like a worthy challenge.