bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

Bug with Dynamic rigidbody creation

Open rewin123 opened this issue 2 years ago • 1 comments

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);
}

rewin123 avatar Mar 06 '23 09:03 rewin123

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.

flmng0 avatar Apr 14 '23 15:04 flmng0