bevy_rapier3d/pipeline/
query_filter.rs

1use bevy::prelude::Entity;
2
3pub use rapier::pipeline::QueryFilterFlags;
4
5use crate::geometry::CollisionGroups;
6
7/// A filter that describes what collider should be included or excluded from a scene query.
8#[derive(Copy, Clone, Default)]
9pub struct QueryFilter<'a> {
10    /// Flags indicating what particular type of colliders should be excluded.
11    pub flags: QueryFilterFlags,
12    /// If set, only colliders with collision groups compatible with this one will
13    /// be included in the scene query.
14    pub groups: Option<CollisionGroups>,
15    /// If set, the collider attached to that entity will be excluded from the query.
16    pub exclude_collider: Option<Entity>,
17    /// If set, any collider attached to the rigid-body attached to that entity
18    /// will be excluded from the query.
19    pub exclude_rigid_body: Option<Entity>,
20    /// If set, any collider for which this closure returns false.
21    pub predicate: Option<&'a dyn Fn(Entity) -> bool>,
22}
23
24impl From<QueryFilterFlags> for QueryFilter<'_> {
25    fn from(flags: QueryFilterFlags) -> Self {
26        Self {
27            flags,
28            ..QueryFilter::default()
29        }
30    }
31}
32
33impl From<CollisionGroups> for QueryFilter<'_> {
34    fn from(groups: CollisionGroups) -> Self {
35        Self {
36            groups: Some(groups),
37            ..QueryFilter::default()
38        }
39    }
40}
41
42impl<'a> QueryFilter<'a> {
43    /// A query filter that doesn’t exclude any collider.
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Exclude from the query any collider attached to a fixed rigid-body and colliders with no rigid-body attached.
49    pub fn exclude_fixed() -> Self {
50        QueryFilterFlags::EXCLUDE_FIXED.into()
51    }
52
53    /// Exclude from the query any collider attached to a kinematic rigid-body.
54    pub fn exclude_kinematic() -> Self {
55        QueryFilterFlags::EXCLUDE_KINEMATIC.into()
56    }
57
58    /// Exclude from the query any collider attached to a dynamic rigid-body.
59    pub fn exclude_dynamic() -> Self {
60        QueryFilterFlags::EXCLUDE_DYNAMIC.into()
61    }
62
63    /// Excludes all colliders not attached to a dynamic rigid-body.
64    pub fn only_dynamic() -> Self {
65        QueryFilterFlags::ONLY_DYNAMIC.into()
66    }
67
68    /// Excludes all colliders not attached to a kinematic rigid-body.
69    pub fn only_kinematic() -> Self {
70        QueryFilterFlags::ONLY_KINEMATIC.into()
71    }
72
73    /// Exclude all colliders attached to a non-fixed rigid-body
74    /// (this will not exclude colliders not attached to any rigid-body).
75    pub fn only_fixed() -> Self {
76        QueryFilterFlags::ONLY_FIXED.into()
77    }
78
79    /// Exclude from the query any collider that is a sensor.
80    pub fn exclude_sensors(mut self) -> Self {
81        self.flags |= QueryFilterFlags::EXCLUDE_SENSORS;
82        self
83    }
84
85    /// Exclude from the query any collider that is not a sensor.
86    pub fn exclude_solids(mut self) -> Self {
87        self.flags |= QueryFilterFlags::EXCLUDE_SOLIDS;
88        self
89    }
90
91    /// Only colliders with collision groups compatible with this one will
92    /// be included in the scene query.
93    pub fn groups(mut self, groups: CollisionGroups) -> Self {
94        self.groups = Some(groups);
95        self
96    }
97
98    /// Set the collider that will be excluded from the scene query.
99    pub fn exclude_collider(mut self, collider: Entity) -> Self {
100        self.exclude_collider = Some(collider);
101        self
102    }
103
104    /// Set the rigid-body that will be excluded from the scene query.
105    pub fn exclude_rigid_body(mut self, rigid_body: Entity) -> Self {
106        self.exclude_rigid_body = Some(rigid_body);
107        self
108    }
109
110    /// Set the predicate to apply a custom collider filtering during the scene query.
111    pub fn predicate(mut self, predicate: &'a impl Fn(Entity) -> bool) -> Self {
112        self.predicate = Some(predicate);
113        self
114    }
115}