Drift Counteraction Optimal Control: Theory and Applications to Autonomous Cars and Spacecraft