  Dissipative environments are ubiquitous in nature, from microscopic swimmers
in low-Reynolds-number fluids to macroscopic animals in frictional media. In
this study, motivated by various behaviours of {\it Caenorhabditis elegans}
during swimming and crawling locomotion, we consider a mathematical model of a
slender elastic locomotor with an internal rhythmic neural pattern generator.
By analysing the dynamical systems of the model using a Poincar\'e section, we
found that local neuromechanical adjustments to the environment can create
robust undulatory locomotion. This progressive behaviour emerges as a global
stable periodic orbit in a broad range of parameter regions. Further, by
controlling the mechanosensation, we were able to design the dynamical systems
to manoeuvre with progressive, reverse, and turning motions as well as
apparently random, complex behaviours, as experimentally observed in {\it C.
elegans}. The mechanisms found in this study, together with our methodologies
with the dynamical systems viewpoint, are useful for deciphering complex animal
adaptive behaviours and also designing adaptive robots for a wide range of
dissipative environments.
