dimforge / bevy_rapier

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

Rigid body motion stops abruptly #240

Closed Netzwerk2 closed 2 years ago

Netzwerk2 commented 2 years ago

When applying a small velocity to a rigid body, it will stop moving after a few seconds (the same happens with rotation). Here's a minimal example to reproduce

use bevy::prelude::shape::Cube;
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
        .insert_resource(RapierConfiguration {
            gravity: Vec3::ZERO,
            ..default()
        })
        .add_startup_system(setup)
        .run();
}

fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
    commands
        .spawn_bundle(PbrBundle {
            mesh: meshes.add(Cube::default().into()),
            ..default()
        })
        .insert(RigidBody::Dynamic)
        .insert(Velocity {
            linvel: Vec3::new(0.0, 0.2, 0.0),
            // angvel: Vec3::new(0.2, 0.0, 0.0),
            ..default()
        })
        .insert(AdditionalMassProperties::MassProperties(MassProperties {
            mass: 1.0,
            principal_inertia: Vec3::splat(1.0),
            ..default()
        }));

    commands.spawn_bundle(Camera3dBundle {
        transform: Transform::from_xyz(3.0, 2.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
        ..default()
    });
}
sebcrozet commented 2 years ago

Hi! This is the expected behavior caused by the automatic sleeping system. If a rigid-body moves slowly during some period of time, it will be stopped and "put to sleep" (i.e. stop being simulated until it is moved again or hits something) to save computation times.

You can increase the movement threshold for that sleeping system by inserting the Sleeping component (to the same entity as the rigid-body) with custom threshold values; your you can disable sleeping altogether for that rigid-body.

Netzwerk2 commented 2 years ago

That explains it. Thank you!