What This Is
This is a demonstration of collision detection against a signed distance field, running in Google's Native Client. It is an evolution of another 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.
How It Works
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. You can click the "Show Distance Field" checkbox to see a greyscale visualization of the distance field and you can use the "Show Filtered" checkbox to toggle between a point sampled visualization of the field and a bilinear filtered visualization of the field. Working on this, I can see why people find this a useful technique for text rendering (see this publication from Valve).