What should you use _physics_process for?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey

I’ve been putting movement code and some other things into _physics_process, but it is causing some poor performance. What “synchronization with the physics engine” related code should be going here?

:bust_in_silhouette: Reply From: Zylann

_physics_process should be used for operations that affect physics, such as applying forces to a rigidbody, moving a kinematic body, performing raycasts or other queries dealing with collisions.

It should be preferred because Godot makes sure the physics engine’s state is ready to be used at this point (since it could run in another thread). The other reason is, the physics engine runs with a fixed time step, while the graphics engine runs with a framerate-dependent step that can fluctuate, which physics engines often don’t like.

For performance, I can’t say much without more details.

I’m assuming it’s causing a performance hit but I don’t really know. I base that on the assumption that physics would typically run more times per frame. Is it really useful to put kinematic body motion in there? I can’t envision how it would help if all motion is dependent on custom calculations and the delta scalar.

jarlowrey | 2018-04-05 02:50

Assuming a fixed frame-rate of 1/60 second, I think physics_process is actually called many times less often than process per second.

Diet Estus | 2018-04-05 16:56

Interesting, I thought _process was typically slower. In my game I actually came across a scenario where heavy lag (due to spawning many nodes at once) caused _process to take a long time, which rubber banded movement. So a consistent_physics_process is indeed helpful for Kinematic Bodies

jarlowrey | 2018-04-06 05:58