💾 Archived View for lertsenem.com › 20240619.godot_sleep.gmi captured on 2024-07-08 at 23:44:26. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Sleep parameter in Godot RigidBody2D

I was working on a 2D platformer, and just implemented moving platforms, using `Path2D`, `PathFollow2D`, and `AnimatableBody2D`.

While everything seemed to work as expected at first, here are some issues I ran into.

Platform position does not update

Weirdly, the `PathFollow2D` does not seem to update the platform position. To access it, I have to check the platform `global_position`.

Items floating in the air

I have some `RigidBody2D` items in my game. I tried putting one on a platform moving up and down, and everything was working alright, until the platform started moving down: the item on it just stayed in the air, floating.

Long story short, the reason for that is an optimisation parameter in `RigidBody2D`. In the `Deactivation` subcategory, the `can_sleep` parameter allows the `RigidBody2D` to enter a "sleep" mode when it's not been moving for a while. In this mode the physics of the object won't be updated until it's "woken" by a collision or another force (methods `apply_force()` and `apply_impulse()`). The parameter is activated by default.

Just unchecking the `can_sleep` parameter gives the expected behaviour, but you can maybe do better by tweaking some global parameters.

In your project parameters, in 'Physics → 2D', you can change the three parameters defining the sleep mode for `RigidBody2D`:

Default "Time before Sleep" is 0.5, setting it to a greater value solved my issue while leaving the sleep mode available. 👍