dimforge / bevy_rapier

Official Rapier plugin for the Bevy game engine.
https://rapier.rs
Apache License 2.0
1.19k stars 256 forks source link

Child colliders added by a system seem not to be linked as children in Rapier #252

Open ian-h-chamberlain opened 1 year ago

ian-h-chamberlain commented 1 year ago

Example gist (based on the existing player_movement2 example)

In this example, pressing Space spawns new (unparented) colliders somewhere relative to the player transform, and pressing Return adds all unparented colliders as children of the player. Here is a brief recording of the example:

https://user-images.githubusercontent.com/11131775/187086662-0573b988-1f42-4241-9092-67a8792da3dd.mov

The main player collider does interact with the parentless colliders, (similar to if they were a Fixed Rigidbody, I think?), but once reparented, the colliders don't seem to interact with anything. Also, they are still colored as "parentless" by the debug renderer, which explains why their collisions are not impacting the parent's. This can be verified fairly easily with RapierContext::collider_parent() (none will be found even though there is a Rigidbody on the parent).

Is this expected behavior, or am I doing something wrong in my example? Maybe this is just a gap in the system change detection, and a system is needed that watches for HierarchyEvents on the relevant components, or something like that?

ian-h-chamberlain commented 1 year ago

Interesting, I found a possible workaround by forcing the "initialization" code to run again on the collider after adding it as a child. In the spawning system:

commands.entity(player_entity).add_child(child);
commands.entity(child).remove::<RapierColliderHandle>();

And I ensure the system runs before the collider initialization:

.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
.add_plugin(RapierDebugRenderPlugin::default())
.add_system_to_stage(
    PhysicsStages::SyncBackend,
    player_spawn_control.before(bevy_rapier2d::plugin::systems::init_colliders),
)
.run();

I'm not sure what the consequences might be of running in a different stage like that, (e.g. how it relates to transform propagation or other Bevy default systems), but at least this way I can get the colliders to affect the parent! Presumably this would be a similar mechanism for how this could be done by default (basically, re-evaluate the Collider parent-child relationship whenever the bevy Entity parent-child relationship changes).


Edit: there are definitely some issues with this approach re: transform calculation. I'm having a tough time figuring out how to properly set the collider position vs its transform and in what order, particularly when trying to simultaneously remove an existing rigidbody on the collider entity. This is most obvious if the parent Transform is non-default, especially if it's scaled.

Vrixyz commented 1 month ago

thanks for the details ! Seems related to https://github.com/dimforge/bevy_rapier/issues/486, not entirely sure it's a duplicate though.