dimforge / bevy_rapier

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

Panic when using a bevy mesh as a collider, ccd enabled and scaled transform #234

Closed paul-hansen closed 5 months ago

paul-hansen commented 2 years ago

Description

I'm getting a panic when two objects collide if one has a collider created using from_bevy_mesh and the entity has a scaled transform and the other object has ccd enabled.

thread 'main' panicked at 'internal error: entered unreachable code', C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\parry3d-0.9.0\src\query\nonlinear_time_of_impact\nonlinear_time_of_impact_support_map_support_map.rs:201:40
Click for full traceback ``` thread 'main' panicked at 'internal error: entered unreachable code', C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\parry3d-0.9.0\src\query\nonlinear_time_of_impact\nonlinear_time_of_impact_support_map_support_map.rs:201:40 stack backtrace: 0: std::panicking::begin_panic_handler at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc/library\std\src\panicking.rs:584 1: core::panicking::panic_fmt at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc/library\core\src\panicking.rs:142 2: core::panicking::panic at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc/library\core\src\panicking.rs:48 3: parry3d::query::nonlinear_time_of_impact::nonlinear_time_of_impact_support_map_support_map::compute_toi at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\parry3d-0.9.0\src\query\nonlinear_time_of_impact\nonlinear_time_of_impact_support_map_support_map.rs:201 4: parry3d::query::nonlinear_time_of_impact::nonlinear_time_of_impact_support_map_support_map::nonlinear_time_of_impact_support_map_support_map > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\parry3d-0.9.0\src\query\nonlinear_time_of_impact\nonlinear_time_of_impact_composite_shape_shape.rs:142 6: parry3d::partitioning::qbvh::qbvh::QBVH::traverse_best_first > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\dynamics\ccd\toi_entry.rs:131 11: rapier3d::dynamics::ccd::ccd_solver::impl$1::predict_impacts_at_next_positions::closure$0 at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\dynamics\ccd\ccd_solver.rs:319 12: core::ops::function::impls::impl$3::call_mut >,rapier3d::dynamics::ccd::ccd_solver::impl$1::predict_impacts_at_next_positions::closure_env$0> at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:290 13: parry3d::query::visitors::bounding_volume_intersections_visitor::impl$1::visit > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\parry3d-0.9.0\src\query\visitors\bounding_volume_intersections_visitor.rs:42 14: parry3d::partitioning::qbvh::qbvh::QBVH::traverse_depth_first_with_stack::traverse_depth_first at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\pipeline\query_pipeline.rs:683 17: rapier3d::dynamics::ccd::ccd_solver::CCDSolver::predict_impacts_at_next_positions at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\dynamics\ccd\ccd_solver.rs:283 18: rapier3d::pipeline::physics_pipeline::PhysicsPipeline::run_ccd_motion_clamping at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\pipeline\physics_pipeline.rs:329 19: rapier3d::pipeline::physics_pipeline::PhysicsPipeline::step at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\rapier3d-0.14.0\src\pipeline\physics_pipeline.rs:562 20: bevy_rapier3d::plugin::context::RapierContext::step_simulation at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_rapier3d-0.16.0\src\plugin\context.rs:237 21: bevy_rapier3d::plugin::systems::step_simulation > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_rapier3d-0.16.0\src\plugin\systems.rs:603 22: core::ops::function::FnMut::call_mut,bevy_ecs::system::system_param::Res,bevy_ecs::system::system_param:: at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:164 23: core::ops::function::impls::impl$3::call_mut,bevy_ecs::system::system_param::Res,bevy_ecs::system::system_p at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:290 24: bevy_ecs::system::function_system::impl$30::run::call_inner,bevy_ecs::change_detection::ResMut,bevy_ecs::system::system_param::Res,bevy_ecs: at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\system\function_system.rs:564 25: bevy_ecs::system::function_system::impl$30::run,void (*)(bevy_ecs::change_detection::ResMut,bevy_ecs::system::system_param::Res,bevy_ecs::sy at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\system\function_system.rs:567 26: bevy_ecs::system::function_system::impl$6::run_unsafe,tuple$<>,tuple$,bevy_ecs::system::system_param::Res at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\system\function_system.rs:403 27: bevy_ecs::schedule::executor_parallel::impl$2::prepare_systems::async_block$0 at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\schedule\executor_parallel.rs:194 28: core::future::from_generator::impl$1::poll > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\future\mod.rs:91 29: async_executor::impl$4::spawn::async_block$0,core::future::from_generator::GenFuture > > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\async-executor-1.4.1\src\lib.rs:144 30: core::future::from_generator::impl$1::poll,core::future::from_generator::GenFuture > > > > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\future\mod.rs:91 31: async_task::raw::RawTask,core::future::from_generator::GenFuture > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_tasks-0.8.0\src\task_pool.rs:201 35: std::thread::local::LocalKey::try_with >,alloc::vec::Vec::with >,alloc::vec::Vec > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_tasks-0.8.0\src\task_pool.rs:148 38: bevy_ecs::schedule::executor_parallel::impl$1::run_systems at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\schedule\executor_parallel.rs:126 39: bevy_ecs::schedule::stage::impl$1::run at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\schedule\stage.rs:884 40: bevy_ecs::schedule::Schedule::run_once at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\schedule\mod.rs:342 41: bevy_ecs::schedule::impl$1::run at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.8.0\src\schedule\mod.rs:360 42: bevy_app::app::App::update at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.8.0\src\app.rs:119 43: bevy_winit::winit_runner_with::closure$0 at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.8.0\src\lib.rs:618 44: winit::platform_impl::platform::event_loop::impl$2::run_return::closure$0,bevy_winit::winit_runner_with::closure_env$0> at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:206 45: alloc::boxed::impl$45::call_mut > >,ref_mut$ > >,dyn$ > >,ref_mut$ > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\alloc\src\boxed.rs:1879 46: winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0 > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:245 47: core::panic::unwind_safe::impl$23::call_once,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0 > > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\panic\unwind_safe.rs:271 48: std::panicking::try::do_call > >,tuple$<> > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panicking.rs:492 49: hashbrown::raw::sse2::Group::load 50: std::panicking::try,core::panic::unwind_safe::AssertUnwindSafe > > > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panicking.rs:456 51: std::panic::catch_unwind > >,tuple$<> > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panic.rs:137 52: winit::platform_impl::platform::event_loop::runner::EventLoopRunner >::catch_unwind,tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0 > > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152 53: winit::platform_impl::platform::event_loop::runner::EventLoopRunner >::call_event_handler > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:239 54: winit::platform_impl::platform::event_loop::runner::EventLoopRunner >::move_state_to > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:341 55: winit::platform_impl::platform::event_loop::runner::EventLoopRunner >::main_events_cleared > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:227 56: winit::platform_impl::platform::event_loop::flush_paint_messages > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:726 57: winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0 > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2091 58: core::ops::function::FnOnce::call_once >,tuple$<> > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:248 59: core::panic::unwind_safe::impl$23::call_once > > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\panic\unwind_safe.rs:271 60: std::panicking::try::do_call > >,isize> at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panicking.rs:492 61: hashbrown::raw::sse2::Group::load 62: std::panicking::try > > > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panicking.rs:456 63: std::panic::catch_unwind > >,isize> at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\std\src\panic.rs:137 64: winit::platform_impl::platform::event_loop::runner::EventLoopRunner >::catch_unwind,isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0 > > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152 65: winit::platform_impl::platform::event_loop::thread_event_target_callback > at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2271 66: CallWindowProcW 67: EnumChildWindows 68: IsIconic 69: KiUserCallbackDispatcher 70: NtUserDispatchMessage 71: DispatchMessageW 72: winit::platform_impl::platform::event_loop::EventLoop >::run_return,bevy_winit::winit_runner_with::closure_env$0> at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:221 73: winit::platform_impl::platform::event_loop::EventLoop >::run,bevy_winit::winit_runner_with::closure_env$0> at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:191 74: winit::event_loop::EventLoop >::run,bevy_winit::winit_runner_with::closure_env$0> at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\event_loop.rs:154 75: bevy_winit::run at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.8.0\src\lib.rs:240 76: bevy_winit::winit_runner_with at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.8.0\src\lib.rs:663 77: bevy_winit::winit_runner at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.8.0\src\lib.rs:280 78: core::ops::function::Fn::call > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:77 79: alloc::boxed::impl$46::call,dyn$,assoc$ > > >,alloc::alloc::Global> at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\alloc\src\boxed.rs:1886 80: bevy_app::app::App::run at C:\Users\Paul\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.8.0\src\app.rs:135 81: bevy_rapier_ccd_unreachable_bug::main at .\src\main.rs:5 82: core::ops::function::FnOnce::call_once > at /rustc/a8314ef7d0ec7b75c336af2c9857bfaf43002bfc\library\core\src\ops\function.rs:248 ```

My use case is I have an asteroid field that has randomly placed asteroids with random scales. I have a custom collider mesh for the asteroids and I want my bullets that shoot the asteroids to use ccd so they don't go through the asteroids.

System

Windows 11 Bevy 0.8 bevy_rapier3d 0.16 (also main branch)

Minimal reproduction

This code demonstrates the error, it creates a fixed rigid body using a bevy mesh as a collider and a transform scaled to 2X, then creates a dynamic rigid body with ccd enabled and sends it at it with a high velocity.

use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
        .add_startup_system(setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands
        .spawn()
        .insert_bundle(TransformBundle::from_transform(
            Transform::from_xyz(0.0, 0.0, 20.0)
                // Commenting out this line it no longer panics
                .with_scale(Vec3::splat(2.0)),
        ))
        .insert(
            // If you use a different collider that isn't a bevy mesh here it no longer panics
            Collider::from_bevy_mesh(
                &Mesh::from(shape::Cube { size: 3.0 }),
                &ComputedColliderShape::TriMesh,
            )
            .unwrap(),
        )
        .insert(RigidBody::Fixed);

    commands
        .spawn()
        .insert(Collider::ball(1.0))
        .insert(RigidBody::Dynamic)
        // Commenting out this line it no longer panics
        .insert(Ccd::enabled())
        .insert(Velocity::linear(Vec3::Z * 10000.0));
}
janhohenheim commented 1 year ago

I think there's a regression in rapier 0.21. Getting this error for the first time now in an otherwise unchanged codebase (other than upgrading to Bevy 0.10) Interestingly, it only happens on Wasm and not native.

paul-hansen commented 1 year ago

Just tried this again to see if anything has changed with the new versions and the example I provided still consistently panics, hitting this unreachable!() line.

Here's an updated example:

[dependencies]
bevy = "0.10.0"
bevy_rapier3d = "0.21.0"
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
        .add_startup_system(setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn((
        TransformBundle::from_transform(
            Transform::from_xyz(0.0, 0.0, 20.0)
                // Commenting out this line it no longer panics
                .with_scale(Vec3::splat(2.0)),
        ),
        // If you use a different collider that isn't a bevy mesh here it no longer panics
        Collider::from_bevy_mesh(
            &Mesh::from(shape::Cube { size: 3.0 }),
            &ComputedColliderShape::TriMesh,
        )
        .unwrap(),
        RigidBody::Fixed,
    ));

    commands.spawn((
        Collider::ball(1.0),
        RigidBody::Dynamic,
        // Commenting out this line it no longer panics
        // Ccd::enabled(),
        Velocity::linear(Vec3::Z * 10000.0),
    ));
}
janhohenheim commented 1 year ago

As for me, my code magically works now. Strange 🤷‍♂️

marcelchampagne commented 1 year ago

Also ran into this same issue.

hgeist2 commented 1 year ago

It only happens when i use ComputedColliderShape::TriMesh, if i switch to ComputedColliderShape::ConvexDecomposition(VHACDParameters::default()), it stops

1755 commented 7 months ago

Have the same issue with CCD and TriMesh (and WASM):

bevy = "0.13.0" bevy_rapier3d = { version = "0.25.0", features = [ "enhanced-determinism", "debug-render-3d", "wasm-bindgen", "serde-serialize" ] }

parry3d-0.13.6/src/query/nonlinear_time_of_impact/nonlinear_time_of_impact_support_map_support_map.rs:201:40:
internal error: entered unreachable code
Vrixyz commented 5 months ago

Recent duplicate here: https://github.com/dimforge/bevy_rapier/issues/386

paul-hansen commented 5 months ago

Updated example for bevy 0.13 and bevy_rapier3d 0.26 no longer panics but returns the error:

ERROR log: Closest points not found despite setting the max distance to infinity. 
main.rs ```rust use bevy::{math::vec3, prelude::*}; use bevy_rapier3d::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(RapierPhysicsPlugin::::default()) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(( TransformBundle::from_transform( Transform::from_xyz(0.0, 0.0, 20.0) // Commenting out this line it no longer panics .with_scale(Vec3::splat(2.0)), ), // If you use a different collider that isn't a bevy mesh here it no longer panics Collider::from_bevy_mesh( &Mesh::from(Cuboid { half_size: vec3(1.5, 1.5, 1.5), }), &ComputedColliderShape::TriMesh, ) .unwrap(), RigidBody::Fixed, )); commands.spawn(( Collider::ball(1.0), RigidBody::Dynamic, // Commenting out this line it no longer panics Ccd::enabled(), Velocity::linear(Vec3::Z * 10000.0), )); } ```
Cargo.toml ```toml [package] name = "bevy_rapier_bug_test" version = "0.1.0" edition = "2021" [dependencies] bevy = { version = "0.13.2", features=["wayland"] } bevy_rapier3d = "0.26.0" ```
full log including updated system specs ``` 2024-05-24T15:35:14.760568Z INFO bevy_winit::system: Creating new window "App" (0v1) 2024-05-24T15:35:15.052064Z INFO bevy_render::renderer: AdapterInfo { name: "NVIDIA GeForce RTX 3080", vendor: 4318, device: 8726, device_type: Discre teGpu, driver: "NVIDIA", driver_info: "555.42.02", backend: Vulkan } 2024-05-24T15:35:15.300280Z INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "Linux rolling EndeavourOS", kerne l: "6.9.1-arch1-1", cpu: "AMD Ryzen 9 7950X 16-Core Processor", core_count: "16", memory: "62.0 GiB" } 2024-05-24T15:35:15.305132Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305139Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305141Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305170Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305172Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305173Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305178Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305180Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305182Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305186Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305188Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305190Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305198Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305199Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305204Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305205Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305207Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305211Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:15.305213Z ERROR log: Closest points not found despite setting the max distance to infinity. 2024-05-24T15:35:26.825476Z INFO bevy_window::system: No windows are open, exiting 2024-05-24T15:35:26.825743Z INFO bevy_winit::system: Closing window 0v1 ```