Abstract-In this paper, we present an algorithm for planning and control of legged robot locomotion. Given the desired contact sequence, this method generates gaits and dynamic motions for legged robots without resorting to simplified stability criteria. The method uses direct collocation for searching for solutions within the constraint-consistent subspace defined by the robot's contact configuration. For the differential equation constraints of the collocation algorithm, we use the so-called direct dynamics of a constrained multibody system. The dynamics of a legged robot is different for each contact configuration. Our method deals with such a hybrid nature, and it allows for velocity discontinuities when contacts are made. We introduce the projected impact dynamics constraint to enforce consistency during mode switching. We stabilize the plan using an inverse dynamics controller compatible with the optimal feed-forward control of the motion plan. As a whole, this approach reduces the complexity associated with specifying dynamic motions of a floating-base robot under the constant influence of contact forces. We apply this method on a hydraulically-actuated quadruped robot. We show two types of gaits (walking and trotting) as well as diverse jumping motions (forward, sideways, turning) on the real system. The results presented here are one of the few examples of an optimal control problem solved and satisfactorily transferred to a real torquecontrolled legged robot.