dimforge / rapier.rs

Official website for the Rapier physics engine.
https://rapier.rs
17 stars 48 forks source link

The bevy_plugin/advanced_collision_detection page is out-of-date #45

Closed anchpop closed 1 month ago

anchpop commented 1 year ago

Thank you for the amazing library, and the amazing docs! There was just one thing I noticed.

There's this code block on the website:

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

struct MyPhysicsHooks;

impl PhysicsHooksWithQuery<NoUserData<'_>> for MyPhysicsHooks {
    fn modify_solver_contacts(
        &self, 
        context: ContactModificationContextView,
        _user_data: &Query<NoUserData>
    ) {
        // This is a silly example of contact modifier that does silly things
        // for illustration purpose:
        // - Flip all the contact normals.
        // - Delete the first contact.
        // - Set the friction coefficients to 0.3
        // - Set the restitution coefficients to 0.4
        // - Set the tangent velocities to X * 10.0
        *context.normal = -*context.normal;

        if !context.solver_contacts.is_empty() {
            context.solver_contacts.swap_remove(0);
        }

        for solver_contact in &mut *context.solver_contacts {
            solver_contact.friction = 0.3;
            solver_contact.restitution = 0.4;
            solver_contact.tangent_velocity.x = 10.0;
        }

        // Use the persistent user-data to count the number of times
        // contact modification was called for this contact manifold
        // since its creation.
        *context.user_data += 1;
        println!("Contact manifold has been modified {} times since its creation.", *context.user_data);
    }
}

fn setup_physics(mut commands: Commands) {
    // Register our physics hooks.
    commands.insert_resource(PhysicsHooksWithQueryObject(Box::new(MyPhysicsHooks {})));

    // Add colliders with a `CustomFilterTag` component. Only colliders
    // with the same `CustomFilterTag` variant will collider thanks to
    // our custom physics hooks:
    commands.spawn()
        .insert(Collider::ball(0.5))
        .insert(ActiveHooks::MODIFY_SOLVER_CONTACTS);

    // TODO: add other colliders in a similar way.
}

This is out of date in multiple ways. PhysicsHooksWithQueryObject has been renamed to PhysicsHooksWithQueryResource. Also, NoUserData<'_> no longer takes a type parameter. Additionally, ContactModificationContextView has no normal or solver_contacts fields

I'd make a PR and the first two are pretty easy to fix, but the last one requires going from context.normal to context.raw.manifold.data.normal, but that causes an error on the *context.normal = -*context.normal; line:

cannot apply unary operator `-` to type `bevy_rapier3d::nalgebra::coordinates::XYZ<f32>`
paul-hansen commented 1 year ago

Now PhysicsHooksWithQueryResource has been renamed again to BevyPhysicsHooks in https://github.com/dimforge/bevy_rapier/pull/323 and it's usage is significantly different (still figuring it out myself).

I wasn't sure if I should open a new issue since it looks like this one did get addressed for the old changes but was never closed.

Vrixyz commented 1 month ago

Those have been fixed, thanks!