This work investigates the gravity compensation topic, from a control perspective. The gravity could be levelled by a compensating mechanical system or in the control law, such as proportional derivative (PD) plus gravity, sliding mode control, or computed torque method. The gravity compensation term is missing in linear and nonlinear optimal control, in both continuous-and discrete-time domains. The equilibrium point of the control system is usually zero and this makes it impossible to perform regulation when the desired condition is not set at origin or in other cases, where the gravity vector is not zero at the equilibrium point. The system needs a steady-state input signal to compensate for the gravity in those conditions. The stability proof of the gravity compensated control law based on nonlinear optimal control and the corresponding deviation from optimality, with proof, are introduced in this work. The same concept exists in discrete-time control since it uses analog to digital conversion of the system and that includes the gravity vector of the system. The simulation results highlight two important cases, a robotic manipulator and a tilted-rotor hexacopter, as an application to the claimed theoretical statements.