This is a demonstration of collision detection against a signed distance field, running in Google's Native Client. It is an evolution of the last demo I put together of a fluid simulation running in Native Client. Aside from working on making that demo faster I also wanted to give it some more interesting physics by putting the fluid in a more complicated environment than a box. This is a result of working toward that.
So what is actually going on here? To begin with, a signed distance field is a scalar field describing the distance at each point in the field from the nearest point on some surface (or set of surfaces). Points that lie outside of an object have positive distance to the surface while points that lie inside an object will have a negative distance.
Using a signed distance field then makes it very easy to check if a particle is colliding with our environment - simply sample the distance field at the particle position and if the magnitude of the distance is negative we have a collision (ignoring the potential for tunneling for the time being). Once a collision is detected we can then backtrack along the particle trajectory until we find where the particle collided with the surface. To calculate the collision response we then need to determine the surface normal - this is again easy as we simply need to calculate the gradient of the distance field and normalize the result.
Aside from making the the collision detection process rather simple, signed distance fields are also attractive because you can get pretty good results with a pretty low resolution field. In this demo the distance field is only 32x32 samples, or 1/16 the resolution we are rendering at. I have included some visualization options so you can see the distance field both with point sampling and with bilinear filtering (CPU rendered so it is a bit slow). Working on this, I can see why people find this a useful technique for text rendering (see this publication from Valve).
Next up will be re-integrating an optimized fluid simulation and then moving the rendering over to the GPU.