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

Take child collider rotation into account for contact normals #238

Merged
merged 1 commit into from
Nov 11, 2023

Conversation

Jondolf
Copy link
Owner

@Jondolf Jondolf commented Nov 10, 2023

Objective

Currently, local contact normals are only rotated by the rotation of each body, but the rotation of child colliders is not taken into account. This causes collision stability issues and incorrect behavior when child colliders are rotated.

For example, with the code below, the cube falls through the ground as reported by @mbreac on the Bevy Discord:

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

fn setup(
    mut commands: Commands,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    // floor
    commands
        .spawn(PbrBundle {
            mesh: meshes.add(shape::Box::new(4., 0.3, 4.).into()),
            material: materials.add(Color::WHITE.into()),
            ..default()
        })
        .insert(RigidBody::Static)
        .with_children(|b| {
            b.spawn(SpatialBundle {
                transform: Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)),
                ..default()
            })
            .insert(Collider::cuboid(4., 4., 0.3));
        });

    // cube
    commands
        .spawn(PbrBundle {
            mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
            material: materials.add(Color::RED.into()),
            transform: Transform::from_xyz(0.0, 3.5, 0.0),
            ..default()
        })
        .insert(RigidBody::Dynamic)
        .insert(Collider::cuboid(1., 1., 1.));
}

Solution

Rotate the contact normals passed to the solver by the rotation of the participating child collider. This fixes the issues.

@Jondolf Jondolf added bugfix A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on labels Nov 10, 2023
@Jondolf Jondolf merged commit 8ff8aea into main Nov 11, 2023
4 checks passed
@Jondolf Jondolf deleted the fix-child-collider-contact-normals branch November 11, 2023 20:19
@Jondolf Jondolf added C-Bug Something isn't working and removed bugfix labels Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on C-Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant