We describe a methodology to plan the trajectory of a robot moving in a two-dimensional space. The robot has to reach a target avoiding obstacles. We show that if the position of the target and of the obstacles in known a priori, then a Hamiltonian function can be constructed and used to define the trajectory. We consider both the static case, namely the case in which both the target and the obstacles are fixed, and the dynamic case, namely the case in which the target or the obstacles move. We prove that in both cases the target can be reached in finite time. The paper is enriched by several examples that illustrate the discussion.