(only addressing the fall-through-the-floor part, not the go-across-ceiling part which seems something different)
If I understand your algorithm correctly, you do the following:
- Take your box current position t=0
- Take the target position t=1
- For all lines, find the collision time, and take the smallest one, tc.
- If tc < 1, move the box ahead by tc times displacement vector.
What happens is that through numerical error when you move your box one of the sides moves slightly aside and passes a line (typically the ground). From there it doesn't hit anymore and can fall through.
My advice would be to keep a safety margin. Don't let your object move by col.time, in :
b.pos.add(vec.mul(b.vel, delta * col.time * facTime));
Move by (col.time - 0.01). Adjust 0.01 as needed. Don't move if col.time < 0.01.
I'm used to doing this kind of stuff in C++, not .js, so I'm not quite ready to test it in your framework, but I'll try later and check that it works. If you can try it, just le me know whether it helps.
I'm not sure how your velocity changes as you near the object, there might still be an issue if velocity approaches zero.