jcornaz / heron

[DISCONTINUED] An ergonomic physics API for bevy games
MIT License
293 stars 44 forks source link

Child Entities Lag Behind Parents #231

Closed jcornaz closed 2 years ago

jcornaz commented 2 years ago

As reported in the discussion #230:

Using Heron 2.2.0 with 2d feature in bevy 0.6.1:

When creating child entities (with sensors attached, and a visualization component like a circle from the lyon crate), the children lag behind the parent when using velocities to move the parent rigidbody which is of type RigidBody::KinematicVelocityBased.

When creating the same structure directly with rapier2d, the same lagging behind can't be observed, so I assume it has to do with heron directly.

Is that a known issue, or is there something special that needs to be considered when using child entities with rigidbodies?

jakoschiko commented 2 years ago

I observed a similar lag when I tried to sync a camera to a body.

My guess: heron's step systems are responsible for updating bevy's Transform (see here). They are running in CoreStage::PostUpdate (see here). bevy's GlobalTransform are being updated in the same stage. Its doc says:

This system runs in stage CoreStage::PostUpdate. If you update theTransform of an entity in this stage or after, you will notice a 1 frame lag before the GlobalTransform is updated.

jakoschiko commented 2 years ago

I was able to fix my problem with

.add_system_to_stage(
    CoreStage::PostUpdate,
    set_camera_to_player
        // The player's `Transform` is updated here
        .after(PhysicsSystem::TransformUpdate)
        // The camera's `Transform` is used here
        .before(TransformSystem::TransformPropagate),
)
jcornaz commented 2 years ago

Yes, this is the problem (and solution).

I just didn't have time to solve it yet as I was on holiday. Hopefully (but unsure), I may have time this weekend.

If you want to fix it yourself and open a pull request, you can. Just don't forget to add a test ;-)

jcornaz commented 2 years ago

~Actually, after a deeper look, I cannot manage to create a test that reproduces the problem.~

~@Occuros, could you share how to reproduce?~

EDIT: I managed to reproduce, and fix it. I should be able to publish the fix today