Floaty
2D fluid & soft body simulation based on Position Based Dynamics
Install / Use
/learn @matsuoka-601/FloatyREADME
Floaty
Soft body & fluid simulation based on Position Based Dynamics (PBD).

- Position Based Dynamics (PBD) and Position Based Fluids (PBF) is used for simulating soft bodies and fluid.
- For two-way coupling of soft body and fluid simulation, a method described in Unified Particle Physics for Real-Time Applications is used.
- Buoyancy is naturally simulated thanks to this paper.
- The simulation is accelerated with multithreading using wasm-bindgen-rayon.
How to run
npm install
npm run build
npm run dev
If you have trouble running the repo, feel free to open an issue.
Demo video
Overview of the simulation
Soft bodies
Soft bodies are simulated using distance constraint and area constraint.
Distance constraints limit adjacent particles of each soft body from moving too far apart or too close together. And area constraints limit the movement of particles so that soft bodies do not collapse.
For more detail, reading section 3.3 (Constraint Projection) and 4.4 (Cloth Balloons) in the original PBD paper would help.
Fluid
The simulation of fluid is based on Position Based Fluids. That is, $i$ th fluid particle is constrained to satisfy the following density constraint( $\rho_0$ is the target density and $\rho_i$ is the density of $i$ th particle).
$$ C_i(\boldsymbol{p}_1,\dots, \boldsymbol{p}_n) = \frac{\rho_i}{\rho_0}-1=0 $$
My initial implementation is based on the one by Matthias Müller.
To simulate surface tension, cohesion and curvature term is used, which is described in Versatile Surface Tension and Adhesion for SPH Fluids by Akinci et al.
Two-Way coupling of fluid and soft bodies
Following the framework described in Unified Particle Physics for Real-Time Applications, two-way coupling of fluid and soft bodies can be naturally simulated.
In PBF calculations, fluid particles and soft body particles are treated in almost the same way. Buoyancy is automatically achieved using the mass weighted version of PBD (Eq. (4) and (5) in the paper).
Some tricks to stabilize the simulation
Prevent soft bodies from get entangling
The softbodies can easily get entangled when they collide. To prevent this problem, repulsive forces are applied between overlapping soft bodies. They still sometimes get entangled even with this trick, but it's less frequent than it would be without it.
In addition, soft body particles within other soft bodies do not interact with those soft bodies. This also prevents soft bodies from becoming entangled with each other.
Prevent fluid particles from entering soft bodies
To prevent fluid particles from remaining inside soft bodies, fluid particles inside soft bodies do not interact with soft body particles. Fluid particles naturally go out of the soft bodies thanks to this trick.
References
- Position Based Dynamics by Müller et al.
- Position Based Fluids by Macklin et al.
- Unified Particle Physics for Real-Time Applications by Macklin et al.
- Small Steps in Physics Simulation by Macklin et al.

