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

Entities with Multibody joints and colliders cause a panic. #552

Closed
nsjvidana opened this issue Jul 3, 2024 · 0 comments · Fixed by dimforge/rapier#672
Closed

Entities with Multibody joints and colliders cause a panic. #552

nsjvidana opened this issue Jul 3, 2024 · 0 comments · Fixed by dimforge/rapier#672
Labels
A-Dynamics C-Bug Something isn't working D-Medium P-High arbitrary important item

Comments

@nsjvidana
Copy link

nsjvidana commented Jul 3, 2024

When having a serial chain of multibodies, giving only one of the multibody entities in the middle of the chain a collider component ends up crashing the program on the first frame. If you continue to give colliders to entities down the chain though, there won't be any crashing.

So for example, a 4-segment chain given colliders as shown: [no collider, collider, no collider, no collider] will crash.

While 4-segment chain with colliders: [no collider, collider, collider, collider] will not crash.

Bug Repro code

use bevy_rapier3d::plugin::RapierPhysicsPlugin;
use bevy_rapier3d::prelude::{Collider, FixedJointBuilder, MultibodyJoint, RevoluteJointBuilder, RigidBody};
use bevy_rapier3d::render::RapierDebugRenderPlugin;
use bevy::math::Vec3;
use bevy::prelude::*;

fn main() {
    std::env::set_var("RUST_BACKTRACE", "1");
    let mut app = App::new();
    app.add_plugins((
        DefaultPlugins,
        RapierPhysicsPlugin::<()>::default(),
        RapierDebugRenderPlugin::default(),
    ))
        .add_systems(Startup, startup);

    app.run();
}

pub fn startup(
    mut commands: Commands
) {
    //camera
    commands.spawn((
        Camera3dBundle {
            transform: Transform::from_xyz(2., 2., 2.)
                .looking_at(Vec3::ZERO, Vec3::Y),
            ..default()
        },
        Collider::ball(0.1)
    ));

    let fixed = commands.spawn((
        RigidBody::Fixed,
        Transform::default()
    )).id();

    macro_rules! mb_joint {
        ($parent:expr, $joint:expr) => {
            (
                RigidBody::Dynamic,
                Transform::default(),
                MultibodyJoint::new($parent, $joint)
            )
        };
    }

    let half_height = 0.3;
    let radius = 0.05;
    let size = (half_height+radius) * 2.;
    let half_size = size/2.;

    let shld_x = commands.spawn(mb_joint!(fixed, RevoluteJointBuilder::new(Vec3::X))).id();
    let shld_y = commands.spawn(mb_joint!(shld_x, RevoluteJointBuilder::new(Vec3::Y))).id();
    let shld_z = commands.spawn(mb_joint!(shld_y, RevoluteJointBuilder::new(Vec3::Z))).id();

    let upper_arm_collider = commands.spawn((
        RigidBody::Dynamic,
        Collider::capsule_y(half_height, radius),
        MultibodyJoint::new(shld_z, FixedJointBuilder::new().local_anchor2(Vec3::Y * half_size))
    )).id();

    //removing this code prevents the crash
    {
        let elbow_pitch = commands.spawn(mb_joint!(upper_arm_collider, RevoluteJointBuilder::new(Vec3::X)
            .local_anchor1(Vec3::Y * -half_size)
        )).id();
    }
}

Adding a collider to elbow_pitch as so prevents the crash:

let elbow_pitch = commands.spawn(mb_joint!(upper_arm_collider, RevoluteJointBuilder::new(Vec3::X)
        .local_anchor1(Vec3::Y * -half_size)
    )).insert(Collider::ball(0.1)).id();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Dynamics C-Bug Something isn't working D-Medium P-High arbitrary important item
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants