Part of the issue is locking the character to the floor, so that we only need to apply gravity when he isn't grounded. If we just do this naively, only applying gravity if the distance is greater than a threshold, we can end up with a capsule being grounded at different heights, for example when walking off a very shallow ledge, it can end up floating very slightly.
The problem to solve is that this minimum distance from the centre of the bottom sphere of the capsule to the floor varies depending on the angle of the floor.
So after a bit of high tech work in Paint.NET:
We know the normal of the floor, and the radius of the circle.
(0, 1, 0) dot floorNormal is the cosine of the angle between the world up vector and the floor normal. Some simple logic shows that this must be equal to the cosine of a in the image above.
Bit of trig tells us that if we know the cosine of a and the length of x that y is just x / cos(a).
I like it when you see equations leading to physical geometry - at the point at which cos(a) becomes 0, the normal of the floor is orthogonal to the world up vector, in which case it will never be found by a ray cast down from the sphere - reflecting the fact that the distance is undefined at this point.
Da dah! So now we have the correct grounded distance from the centre of the lower sphere to the floor. So now we have a value to lock to if the distance is within a certain threshold, otherwise apply gravity.
So far so good. This seems to work out well for accurately detecting the floor.
More will follow. Victory or death.