bevy_xpbd icon indicating copy to clipboard operation
bevy_xpbd copied to clipboard

Unstable joint when attached body has multiple colliders

Open dafteran4 opened this issue 2 years ago • 0 comments

Hello. I am attaching some code for a configuration of a spherical joint where the second body attached has two colliders. This produces unstable / exploding behavior if one of those colliders has a transform component. I am testing on latest 'main'.

use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;

fn main() {
    App::new()
        .add_plugins((
                DefaultPlugins,
                PhysicsPlugins::default(),
                PhysicsDebugPlugin::default(),
        ))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {

    commands.insert_resource(Gravity(Vec3::NEG_Y * 9.8));
    commands.insert_resource(
        Time::new_with(Physics::fixed_hz(200.0)).with_relative_speed(0.3)
    );

    let mut transform = Transform::from_xyz(0., 1.0, 0.);

    let entity1 = commands
        .spawn((
                RigidBody::Dynamic,
                TransformBundle::from(transform),
        ))
        .with_children(|children| {
            children.spawn((
                    Collider::capsule(0.5, 0.1),
            ));
        })
        .id();

    transform = transform * Transform::from_xyz(0., 0.4, 0.);
    transform = transform * Transform::from_rotation(Quat::from_rotation_z(0.2));
    transform = transform * Transform::from_xyz(0., 0.4, 0.);

    let entity2 = commands
        .spawn((
                RigidBody::Dynamic,
                TransformBundle::from(transform),
        ))
        .with_children(|children| {
            children.spawn((
                    Collider::capsule(0.5, 0.1),
            ));
            children.spawn((
                    Collider::cuboid(0.5, 0.3, 0.3),
                    // NOTE: the transform is problematic and causes exploding behavior, if
                    // omitted, everything works fine
                    Transform::from_xyz(0., 0.7, 0.),
            ));
        })
        .id();

    commands.spawn(
        SphericalJoint::new(entity1, entity2)
        .with_local_anchor_1(Vec3::new(0., 0.4, 0.))
        .with_local_anchor_2(Vec3::new(0., -0.4, 0.))
    );

    commands.spawn((RigidBody::Static, Collider::cuboid(7.0, 0.25, 7.0)));

    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0., 2., 10.).looking_at(Vec3::ZERO, Vec3::Y),
        ..default()
    });

}

dafteran4 avatar Nov 21 '23 10:11 dafteran4