bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

Feature request: ContactPairView::other_collider

Open cha0s opened this issue 3 years ago • 2 comments

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? :)

cha0s avatar Jul 14 '22 18:07 cha0s

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).

sebcrozet avatar Jul 17 '22 16:07 sebcrozet

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

chungwong avatar Jul 21 '22 02:07 chungwong