dimforge / bevy_rapier

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

Bug with Dynamic rigidbody creation #337

Open rewin123 opened 1 year ago

rewin123 commented 1 year ago

I noticed that if I first add a Collider component to an object, and then I add a RigidBody::Dynamic component to the same object in another render frame, then the added Collider is not used when calculating the Rigidbody. (Or created rigidbody is static)

\\create child of ship before playing
let id = cmds.spawn(SceneBundle {
    scene: asset_server.load(&owned_path),
    ..default()
})
.insert(instance.clone())
.insert(InstanceRotate::default()).id();

let collider_pos = -instance.origin.clone() * bbox.as_vec3() / 2.0 * VOXEL_SIZE;

let collider = ColliderBuilder::cuboid(
    bbox.x as f32 * VOXEL_SIZE / 2.0, 
    bbox.y as f32 * VOXEL_SIZE / 2.0, 
    bbox.z as f32 * VOXEL_SIZE / 2.0
).translation(collider_pos.into()).build();

cmds.entity(id)
    .insert(Collider::from(collider.shared_shape().clone()));
//add rigidbody to ship when player click Play button
let (ship_e, ship, ship_children) = ships.get_mut(block.ship).unwrap();
cmds.entity(ship_e).insert(RigidBody::Dynamic);
for child in ship_children.iter() {
          cmds.entity(*child).insert(RigidBody::Dynamic);
}
flmng0 commented 1 year ago

Hey, I was experiencing a similar issue, having an object that only gains a collider after some amount of time.

If you change the sleeping value of the Sleeping component (from bevy_rapier) to false, then it will "wake up".

Example:

fn make_dynamic(
    mut commands: Commands,
    mut bodies: Query<(Entity, &mut Sleeping), With<WantsToBeDynamic>>,
) {
    for (entity, mut sleeping) in &mut bodies {
        commands.entity(entity).insert(RigidBody::Dynamic);
        sleeping.sleeping = false;
    }
}

Or maybe instead of setting sleeping.sleeping to false, just insert a new Sleeping component when you insert the RigidBody, although I haven't tried that way.

If you use the debug renderer for Bevy Rapier, see if the colour changes after waking it up, then you at least know Rapier is noticing your change.

If the above doesn't work, then I'm sorry, I don't know how to help you.