Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.4k stars 112 forks source link

Handle sleeping logic before physics #425

Closed Jondolf closed 2 months ago

Jondolf commented 2 months ago

Objective

Currently, sleeping and waking is handled after physics. This leads to a one frame delay before bodies are woken up by things like velocity changes or applied external forces. It can even cause ExternalImpulse to do nothing, because the body is only woken up by the time the impulse should've already been applied and cleared.

Solution

Run sleeping logic before physics. User changes are detected by comparing component change ticks to a LastPhysicsTick resource. This refactor also lets us get rid of the PhysicsChangeTicks component, which was previously stored and updated for all physics entities.

PhysicsStepSet::Sleeping now technically doesn't have much sleeping logic anymore, but that can be reworked in a follow-up.