Optimization-based control of hybrid dynamical systems