dimforge / bevy_rapier

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

Feature request: ContactPairView::other_collider #215

Open cha0s opened 2 years ago

cha0s commented 2 years ago

Hi!

In the documentation I saw this example:

        let other_collider = if contact_pair.collider1() == entity {
            contact_pair.collider2()
        } else {
            contact_pair.collider1()
        };

I thought that I'd suggest a method on ContactPairView; other_collider

Disclaimer: I'm just learning rust so this might be bad code. I'd appreciate any feedback.

Anyway, here's the idea code:

impl<'a> ContactPairView<'a> {

    ...

    pub fn other_collider(&self, entity: Entity) -> Option<Entity> {
        if entity == self.collider1() { return Some(self.collider2()); };
        if entity == self.collider2() { return Some(self.collider1()); };
        None
    }
}

Thoughts? :)

sebcrozet commented 2 years ago

Hi! I transferred this issue to the bevy_rapier repository.

I believe this is a good addition. Please, feel free to open a PR. I suggest the implementation to look like this (return isn’t really idiomatic in Rust):

    pub fn other_collider(&self, entity: Entity) -> Option<Entity> {
        if entity == self.collider1() { Some(self.collider2()) }
        else if entity == self.collider2() { Some(self.collider1()) }
        else { None }
    }

(cargot fmt will take care of formatting this properly).

chungwong commented 2 years ago

Would bevy_rapier2d::geometry::CollidingEntities component work for you? https://docs.rs/bevy_rapier2d/latest/bevy_rapier2d/geometry/struct.CollidingEntities.html