bevy_xpbd icon indicating copy to clipboard operation
bevy_xpbd copied to clipboard

Fluid Simulation

Open amytimed opened this issue 2 years ago • 4 comments

fluid simulation

amytimed avatar Nov 29 '23 19:11 amytimed

https://docs.google.com/presentation/d/1fEAb4-lSyqxlVGNPog3G1LZ7UgtvxfRAwR0dwd19G4g/edit?usp=sharing

amytimed avatar Dec 05 '23 13:12 amytimed

Note: I've never implemented fluid simulation before and haven't looked too deeply into the details yet, so some things might be inaccurate.

In general, fluid simulation is very challenging to do in a way that is efficient and flexible enough for usage in games. 2D might be doable, but 3D would be very difficult or even impossible to do in real time at large scales. This is why you don't really see much proper fluid simulation in games apart from very small amounts of water or gases or the "fake" water that isn't actually physically simulated (like heightfield-based water or purely visual water like in Subnautica).

Some challenges

  • Fluid simulation is very expensive. In 3D, a simple pool could have hundreds of thousands of particles, which isn't very feasible to do on the CPU. This is why fluid simulation is often done on the GPU, but that might make interoperability with the ECS difficult (but worth experimenting with).
  • I believe many fluid simulation methods require a fixed grid (for spatial acceleration structures), so fluids would have to be bound to a specific area instead of being able to flow freely anywhere in the world. I'd need to check if there have been advancements here though.
  • Just rendering circles for the particles probably isn't enough for most projects, so there should be rendering based on ray marching. The visual style also varies between applications, so I suppose this should maybe be done by external crates.

Different approaches

  • SPH (Smoothed Particle Hydrodynamics)
  • FLIP fluids (Fluid-Implicit-Particle)
  • MPM (Material Point Method)
    • Not strictly a fluid simulation method, but can simulate various continuum materials (solids, liquids, gases) with multi-phase interactions
    • Used in Frozen for snow :P
  • Other
    • Eulerian fluids
    • Unified particle physics (paper)
      • Would work well with XPBD
      • Easy interactions with rigid bodies, soft bodies, and fluids, because everything is made up of particles
      • Large volumes might be inefficient because it needs to simulate every particle separately instead of treating e.g. a rigid body as a singular object
      • Might require all particles to have the same size to be efficient (because of spatial partitioning)

First steps

SPH and FLIP fluids seem to be the most common approaches, so I'd focus on those. There's nothing that would be specific to bevy_xpbd for the core simulation, so I'd make it a separate crate and explore potential interoperability later on.

I would probably start with a "simple" 2D-only, CPU-based SPH plugin for Bevy, with a goal of having something like Sebastian Lague's video's end result (in 2D). I've seen more learning resources and research on SPH, so it might be easier to get started with in comparison to other methods. Once that's working, it should be pretty easy to extend to 3D, and to support more particles, it could also be worth experimenting with simulation on the GPU.

I'm interested in exploring this field at some point, but unfortunately won't have much time in the foreseeable future due to other physics and collision things being more important for the time being.

I encourage others to try to implement it themselves though! I think fluid simulation is really cool, and it'd be amazing to have a plugin for it in the Bevy ecosystem.

Aside: Salva

Salva is probably the most prominent fluid simulation library in the Rust ecosystem, but it seems to be basically abandoned and uses Nalgebra instead of Glam (not good for Bevy).

It might be a great inspiration and reference though, as it implements different variants of SPH and has some level of interoperability with Rapier. (if only it was updated...)

Jondolf avatar Dec 05 '23 18:12 Jondolf