Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the linear velocity to 0 doesn't stop objects #617

Open
egeres opened this issue Dec 26, 2024 · 2 comments
Open

Setting the linear velocity to 0 doesn't stop objects #617

egeres opened this issue Dec 26, 2024 · 2 comments

Comments

@egeres
Copy link

egeres commented Dec 26, 2024

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?

@Vrixyz
Copy link
Contributor

Vrixyz commented Dec 27, 2024

It comes from the gravity, applied after accounting for the velocity. I'll have to research how gravity is handled in other physics engines, to see if that's expected. In the meantime you can manually add a GravityScale to your object and set it to 0 when you don't want it applied.

@egeres
Copy link
Author

egeres commented Dec 27, 2024

Thanks!! In a way I wanted this because I was experimenting with "large scale mathematical" collision meshes. I guess the way to go is just to generate collision meshes, but that gets very slow when you have complex geometries...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants