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

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

Closed
anchpop opened this issue Sep 3, 2022 · 2 comments
Closed

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

anchpop opened this issue Sep 3, 2022 · 2 comments

Comments

@anchpop
Copy link
Contributor

anchpop commented Sep 3, 2022

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
Copy link

Now PhysicsHooksWithQueryResource has been renamed again to BevyPhysicsHooks in dimforge/bevy_rapier#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
Copy link

Vrixyz commented May 22, 2024

Those have been fixed, thanks!

@Vrixyz Vrixyz closed this as completed May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants