dimforge / bevy_rapier

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

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

Closed nsjvidana closed 4 months ago

nsjvidana commented 4 months ago

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();