optimized gravitation function