Apply gravity only at the end of the update

This way the changes to external forces made by users are not
overwritten.
Improve the ray caster