This is an experiment with using signed distance fields to bound particles into regions. Each region above is represented by a distance field or as a union of several distance fields. In this case, each region's distance field is defined by a function which maps a position in space to the distance to the nearest region boundary.
To keep the particles contained in the regions, the demo uses a janky 2 step gradient descent find the nearest boundary to the offending particle, then moves the particle to that boundary. Then the boundary normal is approximated by taking the gradient of the distance field and the particle's velocity is inverted along the normal.
Written in Typescript because static typing makes me happy.