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

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/dynamics/solver/generic_velocity_constraint.rs:48:49 #110

Closed sofia-m-a closed 2 years ago

sofia-m-a commented 2 years ago

I feel this is a bug, I've tried to fix my code but I can't get it to not panic on certain collisions

Code excerpts:

main

App::new()
        ...
        .add_plugins(DefaultPlugins)
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
        .add_plugin(RapierRenderPlugin)
        ...
        .add_system_set(
            SystemSet::on_enter(GameState::Level)
                .with_system(setup)
                .with_system(setup_player),
        )

setup, setup_player

rapier.scale = TILE_SIZE as f32;
rapier.gravity = Vec2::new(0.0, -12.0).into();

let player_body = RigidBodyBundle {
        position: RigidBodyPositionComponent(RigidBodyPosition {
            position: Vec2::new(0.0, 10.0).into(),
            ..Default::default()
        }),
        velocity: RigidBodyVelocityComponent(RigidBodyVelocity {
            angvel: 0.0,
            linvel: Vec2::new(0.0, 3.0).into(),
        }),
        mass_properties: RigidBodyMassPropsComponent(RigidBodyMassProps {
            flags: RigidBodyMassPropsFlags::ROTATION_LOCKED,
            ..Default::default()
        }),
        ..Default::default()
    };
    let player_shape = ColliderBundle {
        collider_type: ColliderTypeComponent(ColliderType::Solid),
        shape: ColliderShapeComponent(ColliderShape::cuboid(w as f32 / 2.0, h as f32 / 2.0)),
        ..Default::default()
    };

    commands
        .spawn_bundle(SpriteSheetBundle {
            texture_atlas: graphics.p1_texture.clone(),
            ..Default::default()
        })
        ...
        .insert_bundle(player_shape)
        .insert_bundle(player_body)
        .insert(RigidBodyPositionSync::Discrete);

Tile generation

let id = commands
  .spawn()
  .insert_bundle(SpriteSheetBundle { ... })
  .insert_bundle(ColliderBundle {
      shape: ColliderShapeComponent(ColliderShape::cuboid(0.5, 0.5)),
      position: ColliderPositionComponent(ColliderPosition(
        Isometry::translation(p.x as f32, p.y as f32),
      )),
      ..Default::default()
  })
  .insert(ColliderDebugRender::with_id(3))
  .id();
children.push(id);
// -----------
commands
  .spawn()
  .insert(Chunk)
  .insert(Transform::from_translation(Vec3::new(
    p.x as f32 * CHUNK_SIZE as f32 * TILE_SIZE as f32,
    p.y as f32 * CHUNK_SIZE as f32 * TILE_SIZE as f32,
    1.0,
  )))
  .insert(GlobalTransform::default())
  .push_children(&children);

Backtrace

22-01-10T05:08:42.914884Z  INFO bevy_render::renderer: AdapterInfo { name: "AMD RADV RENOIR", vendor: 4098, device: 5686, device_type: IntegratedGpu, backend: Vulkan }
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/dynamics/solver/generic_velocity_constraint.rs:48:49
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/panicking.rs:107:14
   2: core::panicking::panic
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/panicking.rs:48:5
   3: rapier2d::dynamics::solver::generic_velocity_constraint::GenericVelocityConstraint::generate
   4: rapier2d::dynamics::solver::solver_constraints::SolverConstraints<rapier2d::dynamics::solver::velocity_constraint::AnyVelocityConstraint,rapier2d::dynamics::solver::generic_velocity_constraint::GenericVelocityConstraint>::compute_generic_constraints
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/dynamics/solver/solver_constraints.rs:228:13
   5: rapier2d::dynamics::solver::solver_constraints::SolverConstraints<rapier2d::dynamics::solver::velocity_constraint::AnyVelocityConstraint,rapier2d::dynamics::solver::generic_velocity_constraint::GenericVelocityConstraint>::init
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/dynamics/solver/solver_constraints.rs:149:9
   6: rapier2d::dynamics::solver::island_solver::IslandSolver::init_and_solve
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/dynamics/solver/island_solver.rs:85:13
   7: rapier2d::pipeline::physics_pipeline::PhysicsPipeline::build_islands_and_solve_velocity_constraints
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/pipeline/physics_pipeline.rs:249:17
   8: rapier2d::pipeline::physics_pipeline::PhysicsPipeline::step_generic
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.12.0-alpha.0/src/pipeline/physics_pipeline.rs:636:13
   9: bevy_rapier2d::physics::systems::step_world_system
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_rapier2d-0.12.0/src/physics/systems.rs:324:17
  10: core::ops::function::FnMut::call_mut
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/ops/function.rs:150:5
  11: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/ops/function.rs:269:13
  12: <Func as bevy_ecs::system::function_system::SystemParamFunction<(),Out,(F0,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11),()>>::run::call_inner
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/system/function_system.rs:512:21
  13: <Func as bevy_ecs::system::function_system::SystemParamFunction<(),Out,(F0,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11),()>>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/system/function_system.rs:515:17
  14: <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::run_unsafe
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/system/function_system.rs:442:19
  15: bevy_ecs::schedule::executor_parallel::ParallelExecutor::prepare_systems::{{closure}}
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/schedule/executor_parallel.rs:214:30
  16: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/future/mod.rs:84:19
  17: async_executor::Executor::spawn::{{closure}}
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/async-executor-1.4.1/src/lib.rs:144:19
  18: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/future/mod.rs:84:19
  19: async_task::raw::RawTask<F,T,S>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/async-task-4.0.3/src/raw.rs:489:20
  20: async_executor::Executor::try_tick
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/async-executor-1.4.1/src/lib.rs:181:17
  21: bevy_tasks::task_pool::TaskPool::scope::{{closure}}
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_tasks-0.6.0/src/task_pool.rs:222:21
  22: std::thread::local::LocalKey<T>::try_with
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/std/src/thread/local.rs:412:16
  23: std::thread::local::LocalKey<T>::with
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/std/src/thread/local.rs:388:9
  24: bevy_tasks::task_pool::TaskPool::scope
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_tasks-0.6.0/src/task_pool.rs:169:9
  25: <bevy_ecs::schedule::executor_parallel::ParallelExecutor as bevy_ecs::schedule::executor::ParallelSystemExecutor>::run_systems
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/schedule/executor_parallel.rs:122:9
  26: <bevy_ecs::schedule::stage::SystemStage as bevy_ecs::schedule::stage::Stage>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/schedule/stage.rs:850:17
  27: bevy_ecs::schedule::Schedule::run_once
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/schedule/mod.rs:344:13
  28: <bevy_ecs::schedule::Schedule as bevy_ecs::schedule::stage::Stage>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.6.0/src/schedule/mod.rs:362:21
  29: bevy_app::app::App::update
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.6.0/src/app.rs:112:9
  30: bevy_winit::winit_runner_with::{{closure}}
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.6.0/src/lib.rs:504:21
  31: winit::platform_impl::platform::sticky_exit_callback
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.26.0/src/platform_impl/linux/mod.rs:753:5
  32: winit::platform_impl::platform::x11::EventLoop<T>::run_return
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.26.0/src/platform_impl/linux/x11/mod.rs:293:17
  33: winit::platform_impl::platform::x11::EventLoop<T>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.26.0/src/platform_impl/linux/x11/mod.rs:392:9
  34: winit::platform_impl::platform::EventLoop<T>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.26.0/src/platform_impl/linux/mod.rs:669:56
  35: winit::event_loop::EventLoop<T>::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.26.0/src/event_loop.rs:154:9
  36: bevy_winit::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.6.0/src/lib.rs:171:5
  37: bevy_winit::winit_runner_with
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.6.0/src/lib.rs:513:9
  38: bevy_winit::winit_runner
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.6.0/src/lib.rs:211:5
  39: core::ops::function::Fn::call
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/ops/function.rs:70:5
  40: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/alloc/src/boxed.rs:1825:9
  41: bevy_app::app::App::run
             at /home/pree/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.6.0/src/app.rs:130:9
  42: bevy1_1::main
             at ./src/main.rs:30:5
  43: core::ops::function::FnOnce::call_once
             at /rustc/c09a9529c51cde41c1101e56049d418edb07bf71/library/core/src/ops/function.rs:227:5
sebcrozet commented 2 years ago

I confirm this is a bug: you should not be entering this code path.

GetAGripGal commented 2 years ago

Having the same issue, seems it is having trouble with colliders being spawned by different systems. Enabling the parallel feature stops it from crashing but the collisions still don't work and objects pass through each other.

Gnurfos commented 2 years ago

I'm getting a very similar one but with rapier3d, should I create a separate issue?

Meshiest commented 2 years ago

I'm getting this as well with rapier3d with code that did not error in 0.11 (but now does in 0.12)

sebcrozet commented 2 years ago

@Gnurfos No need to create a separate issue, this is the same bug. The bug happens if there is a collider that is not attached to any rigid-body in the scene. This will be fixed within the next couple of days.

Gnurfos commented 2 years ago

I don't think I have colliders without rigid bodies in my case. But let's just wait and see. Thanks!