Many swimming microorganisms inhabit heterogeneous environments consisting of solid particles immersed in viscous fluid. Such environments require the organisms attempting to move through them to negotiate both hydrodynamic forces and geometric constraints. Here, we study this kind of locomotion by first observing the kinematics of the small nematode and model organism Caenorhabditis elegans in fluid-filled, micro-pillar arrays. We then compare its dynamics with those given by numerical simulations of a purely mechanical worm model that accounts only for the hydrodynamic and contact interactions with the obstacles. We demonstrate that these interactions allow simple undulators to achieve speeds as much as an order of magnitude greater than their free-swimming values. More generally, what appears as behavior and sensing can sometimes be explained through simple mechanics.