bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

Setting the linear velocity to 0 doesn't stop objects

Open egeres opened this issue 10 months ago • 2 comments

So I made this dummy code example using

  • bevy 0.15.0
  • bevy_rapier: 0.28.0
use bevy::pbr::MeshMaterial3d;
use bevy::prelude::*;
use bevy::render::mesh::Mesh3d;
use bevy_rapier3d::prelude::*;
use game_flori::camera_0::*;

#[derive(Component)]
struct TheSphere;

fn stop_fall(mut q_sphere: Query<(&mut Velocity, &Transform), With<TheSphere>>) {
    for (mut vel, transform) in q_sphere.iter_mut() {
        let pos = transform.translation;
        if pos.y < 0.0 {
            vel.linvel = Vec3::ZERO;
            continue;
        }
    }
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // A camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
        Projection::from(PerspectiveProjection {
            near: 0.1,
            far: 1000.0,
            fov: 70.0_f32.to_radians(),
            ..default()
        }),
        CameraController::default(),
    ));

    // A sphere
    let sphere_material = materials.add(StandardMaterial {
        base_color: Color::srgb(1.0, 0.0, 0.0),
        ..Default::default()
    });
    commands.spawn((
        Transform::from_xyz(3.0, 15.0, 0.0),
        GlobalTransform::default(),
        Collider::ball(1.0),
        RigidBody::Dynamic,
        Damping {
            linear_damping: 0.0,
            angular_damping: 0.0,
        },
        Restitution::coefficient(0.7),
        Mesh3d(meshes.add(Sphere::new(1.0).mesh())),
        MeshMaterial3d(sphere_material),
        Velocity::default(),
        TheSphere,
    ));

    // A floor or something
    commands.spawn((
        Transform::from_xyz(0.0, 0.0, 0.0),
        GlobalTransform::default(),
        // Collider::cuboid(10.0, 0.2, 10.0),
        Mesh3d(meshes.add(Cuboid::new(10.0, 0.2, 10.0).mesh())),
        MeshMaterial3d(materials.add(StandardMaterial {
            base_color: Color::srgb(0.3, 0.5, 0.3),
            ..Default::default()
        })),
    ));
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
        .add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin)
        .add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin)
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
        .add_plugins(RapierDebugRenderPlugin::default())
        .add_systems(Startup, setup)
        .add_systems(Update, stop_fall)
        .run();
}

The relevant line is vel.linvel = Vec3::ZERO;, in theory the ball should stop past the 0-th height in the Y axis. Instead, it just falls very slowly? I know I could add a collider on the box, but I need this "artificial force setting" for other things. I've been reading the docs but I can't find much info about this... am I missing anything?

egeres avatar Dec 26 '24 22:12 egeres