Skip to content

Rigidbody does not automatically wake up as expected #864

@BenoitRanque

Description

@BenoitRanque

version: 1.0-rc

Description

A clear and concise description of what the bug is.

Rigid bodies doe not wake up automatically when their linear velocity is changed using set_lin_vel()
This is contrary to a comment on set_lin_vel():

Sets new linear velocity of the rigid body. Changing this parameter will wake up the rigid body!

To Reproduce

Steps to reproduce the behavior:

  1. follow the platformer tutorial until character movement is implemented
  2. run the game
  3. click outside the window, then back in. This puts the rigid body to sleep
  4. the character will not move

Character script here:

#[derive(Visit, Reflect, Debug, Clone, Default, TypeUuidProvider, ComponentProvider)]
#[type_uuid(id = "c5671d19-9f1a-4286-8486-add4ebaadaec")]
#[visit(optional)]
pub struct Player {
    sprite: Handle<Rectangle>,
}

impl ScriptTrait for Player {
    fn on_update(&mut self, #[allow(unused_variables)] ctx: &mut ScriptContext) {
        let move_left = ctx.input_state.is_key_down(KeyCode::KeyA);
        let move_right = ctx.input_state.is_key_down(KeyCode::KeyD);
        let jump = ctx.input_state.is_key_pressed(KeyCode::KeyW);

        if let Some(rigid_body) = ctx.scene.graph[ctx.handle].cast_mut::<RigidBody>() {
            // uncomment this to work around the issue
            // if rigid_body.is_sleeping() {
            //     rigid_body.wake_up();
            // }

            // show linear velocity and whether rigid body sleeping
            println!("rigid body sleeping {}, lin_vel: {:?}", rigid_body.is_sleeping(), rigid_body.lin_vel());
            
           let x_speed = if move_left {
                3.0
            } else if move_right {
                -3.0
            } else {
                0.0
            };

            // apply velocity vector accounting for jump
            if jump {
                rigid_body.set_lin_vel(Vector2::new(x_speed, 4.0));
            } else {
                rigid_body.set_lin_vel(Vector2::new(x_speed, rigid_body.lin_vel().y));
            }

            // Flip the sprite if necessary
            let sprite = &mut ctx.scene.graph[self.sprite];
            let is_flipped = sprite.is_flip_x();
            if x_speed > 0.0 && !is_flipped {
                sprite.set_flip_x(true);
            } else if x_speed < 0.0 && is_flipped {
                sprite.set_flip_x(false);
            }
        }
    }
}

Expected behavior

A clear and concise description of what you expected to happen.

Rigid body should wake up when the linear velocity is set.
Additionally, there is a checkbox in the editor "can sleep", which does not seem to work as intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions