bevy_xpbd icon indicating copy to clipboard operation
bevy_xpbd copied to clipboard

Panic when simultaneously querying for ```LinearVelocity``` and ```Forces```

Open stevehello166 opened this issue 3 weeks ago • 2 comments
trafficstars

Avian version and features

avian3d 0.4.0 bevy 0.17.2

[Optional] Relevant system information

System: Kernel: 6.8.0-86-generic arch: x86_64 bits: 64 compiler: gcc v: 13.3.0 clocksource: tsc Desktop: Cinnamon v: 6.4.8 tk: GTK v: 3.24.41 wm: Muffin v: 6.4.1 vt: 7 dm: LightDM v: 1.30.0 Distro: Linux Mint 22.2 Zara base: Ubuntu 24.04 noble CPU: Info: quad core model: Intel Core i5-2500 bits: 64 type: MCP smt: arch: Sandy Bridge rev: 7 cache: L1: 256 KiB L2: 1024 KiB L3: 6 MiB Speed (MHz): avg: 1596 min/max: 1600/3700 cores: 1: 1596 2: 1596 3: 1596 4: 1596 bogomips: 26334 Flags: avx ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 Graphics: Device-1: NVIDIA TU116 [GeForce GTX 1660 SUPER] vendor: ASUSTeK driver: nvidia v: 570.195.03 arch: Turing pcie: speed: 5 GT/s lanes: 16 ports: active: none off: DP-1,DVI-D-1 empty: HDMI-A-1 bus-ID: 01:00.0 chip-ID: 10de:21c4 class-ID: 0300 Display: x11 server: X.Org v: 21.1.11 with: Xwayland v: 23.2.6 driver: X: loaded: nvidia unloaded: fbdev,modesetting,nouveau,vesa gpu: nvidia,nvidia-nvswitch display-ID: :0 screens: 1 Info: Memory: total: 16 GiB available Compilers: clang: 18.1.3 gcc: 13.3.0 alt: 12 Client: Cinnamon v: 6.4.8 inxi: 3.3.34

If you cannot get Bevy to build or run on your machine, please include:

cargo 1.90.0 (840b83a10 2025-07-30)

What you did

I was making a vehicle controller that used the current LinearVelocity to decide how much force to apply to the vehicle. After some time troubleshooting I narrowed it down to the queries and made a minimal project to demonstrate the panic (Attached below). The project compiles perfectly fine, but panics.

use avian3d::prelude::*;
use bevy::prelude::*;

fn main() {
    App::new()
        // Enable physics
        .add_plugins((DefaultPlugins, PhysicsPlugins::default()))
        .add_systems(Startup, setup)
        .add_systems(Update, broken_query)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // Static physics object with a collision shape
    commands.spawn((
        RigidBody::Static,
        Collider::cylinder(4.0, 0.1),
        Mesh3d(meshes.add(Cylinder::new(4.0, 0.1))),
        MeshMaterial3d(materials.add(Color::WHITE)),
    ));

    // Dynamic physics object with a collision shape and initial angular velocity
    commands.spawn((
        RigidBody::Dynamic,
        Collider::cuboid(1.0, 1.0, 1.0),
        AngularVelocity(Vec3::new(2.5, 3.5, 1.5)),
        Mesh3d(meshes.add(Cuboid::from_length(1.0))),
        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
        Transform::from_xyz(0.0, 4.0, 0.0),
            ));

    // Light
    commands.spawn((
        PointLight {
            shadows_enabled: true,
            ..default()
        },
        Transform::from_xyz(4.0, 8.0, 4.0),
    ));

    // Camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Dir3::Y),
    ));
}


fn broken_query(mut query: Query<(&LinearVelocity, Forces)>) {
    for _i in &mut query {
        println!("This should function")
    }
}

What went wrong

That code panics when it should not stating that mutable component access must be unique.

Additional information

The compiler log from that test project was

thread 'main' panicked at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/fetch.rs:1950:9:
&mut avian3d::dynamics::rigid_body::LinearVelocity conflicts with a previous access in this query. Mutable component access must be unique.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
zsh: segmentation fault (core dumped)  cargo run

Running with the backtrace yields this

thread 'main' panicked at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/fetch.rs:1950:9:
&mut avian3d::dynamics::rigid_body::LinearVelocity conflicts with a previous access in this query. Mutable component access must be unique.
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:75:14
   2: bevy_ecs::query::fetch::<impl bevy_ecs::query::world_query::WorldQuery for &mut T>::update_component_access
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/fetch.rs:1950:9
   3: avian3d::dynamics::rigid_body::forces::query_data::_::<impl bevy_ecs::query::world_query::WorldQuery for avian3d::dynamics::rigid_body::forces::query_data::Forces>::update_component_access
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/avian3d-0.4.0/src/dynamics/rigid_body/forces/query_data.rs:105:10
   4: <(F0,F1) as bevy_ecs::query::world_query::WorldQuery>::update_component_access
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/world_query.rs:205:19
   5: bevy_ecs::query::state::QueryState<D,F>::from_states_uninitialized
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/state.rs:213:9
   6: bevy_ecs::query::state::QueryState<D,F>::new_uninitialized
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/state.rs:186:9
   7: bevy_ecs::query::state::QueryState<D,F>::new
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/query/state.rs:164:25
   8: <bevy_ecs::system::query::Query<D,F> as bevy_ecs::system::system_param::SystemParam>::init_state
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/system_param.rs:340:9
   9: <(P,) as bevy_ecs::system::system_param::SystemParam>::init_state
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/system_param.rs:2124:21
  10: <bevy_ecs::system::function_system::FunctionSystem<Marker,Out,F> as bevy_ecs::system::system::System>::initialize::{{closure}}
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/function_system.rs:763:20
  11: core::option::Option<T>::get_or_insert_with
             at /home/joseph/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1802:26
  12: <bevy_ecs::system::function_system::FunctionSystem<Marker,Out,F> as bevy_ecs::system::system::System>::initialize
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/function_system.rs:762:32
  13: bevy_ecs::schedule::node::Systems::initialize
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/schedule/node.rs:569:43
  14: bevy_ecs::schedule::schedule::ScheduleGraph::initialize
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/schedule/schedule.rs:961:22
  15: bevy_ecs::schedule::schedule::Schedule::initialize
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/schedule/schedule.rs:517:24
  16: bevy_ecs::schedule::schedule::Schedule::run
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/schedule/schedule.rs:481:14
  17: bevy_ecs::world::World::try_run_schedule::{{closure}}
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:3600:61
  18: bevy_ecs::world::World::try_schedule_scope
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:3533:21
  19: bevy_ecs::world::World::try_run_schedule
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:3600:14
  20: bevy_app::main_schedule::Main::run_main::{{closure}}
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_app-0.17.2/src/main_schedule.rs:296:31
  21: bevy_ecs::world::World::try_resource_scope
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:2626:22
  22: bevy_ecs::world::World::resource_scope
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:2589:14
  23: bevy_app::main_schedule::Main::run_main
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_app-0.17.2/src/main_schedule.rs:294:15
  24: core::ops::function::FnMut::call_mut
             at /home/joseph/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:168:5
  25: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
             at /home/joseph/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:301:21
  26: <Func as bevy_ecs::system::exclusive_function_system::ExclusiveSystemParamFunction<fn(F0) .> Out>>::run::call_inner
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/exclusive_function_system.rs:270:21
  27: <Func as bevy_ecs::system::exclusive_function_system::ExclusiveSystemParamFunction<fn(F0) .> Out>>::run
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/exclusive_function_system.rs:273:17
  28: <bevy_ecs::system::exclusive_function_system::ExclusiveFunctionSystem<Marker,Out,F> as bevy_ecs::system::system::System>::run_unsafe::{{closure}}
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/exclusive_function_system.rs:135:33
  29: bevy_ecs::world::World::last_change_tick_scope
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/world/mod.rs:3009:9
  30: <bevy_ecs::system::exclusive_function_system::ExclusiveFunctionSystem<Marker,Out,F> as bevy_ecs::system::system::System>::run_unsafe
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/exclusive_function_system.rs:113:15
  31: bevy_ecs::system::system::System::run_without_applying_deferred
             at /home/joseph/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.17.2/src/system/system.rs:139:23
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
zsh: segmentation fault (core dumped)  cargo run

stevehello166 avatar Oct 28 '25 22:10 stevehello166