Skip to main content

crcbl_phys/system/
collision_filter.rs

1//! [`PhysicsSystem`]'s collision filter: which bodies the contact solver lets
2//! touch, by category and mask, as Box2D's `b2Filter` does.
3//!
4//! Every body is in one or more categories and collides with the categories
5//! its mask names; a pair collides only when each one's category is in the
6//! other's mask. A game sorts its bodies once — characters, loose items,
7//! corpses — instead of ignoring pairs one at a time, which is
8//! [`PhysicsSystem::ignore_contacts`]' job for the odd pair no rule covers.
9//!
10//! This filters the solver only. Queries have their own filter, the layers of
11//! [`PhysicsSystem::set_collider_layers`]: a corpse can be solid to the
12//! floor, see-through to loose items and still struck by a bullet ray. Planes
13//! are world geometry and collide with every body.
14
15use crcbl_ecs::Entity;
16
17use super::PhysicsSystem;
18
19/// A body's categories and the categories it collides with.
20#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
21pub struct CollisionFilter {
22    /// The categories the body is in, one bit each.
23    pub category: u32,
24    /// The categories it collides with.
25    pub mask: u32,
26}
27
28impl CollisionFilter {
29    /// The first category, colliding with every category: what every body
30    /// starts with, so a system that sets no filter collides as before.
31    pub const DEFAULT: Self = Self {
32        category: 1,
33        mask: u32::MAX,
34    };
35
36    /// Whether a body with this filter and one with `other` collide.
37    #[must_use]
38    pub const fn collides_with(self, other: Self) -> bool {
39        self.category & other.mask != 0 && other.category & self.mask != 0
40    }
41}
42
43impl Default for CollisionFilter {
44    fn default() -> Self {
45        Self::DEFAULT
46    }
47}
48
49impl PhysicsSystem {
50    /// Sets the categories `entity`'s body is in and those it collides with,
51    /// and wakes it. Contacts the new filter forbids end at once; pairs it
52    /// newly allows are found on the next step. Returns `false`, changing
53    /// nothing, if the entity is not registered.
54    pub fn set_collision_filter(&mut self, entity: Entity, filter: CollisionFilter) -> bool {
55        let Some(&id) = self.entity_to_body.get(&entity) else {
56            return false;
57        };
58        let Some(record) = self.records.get_mut(id) else {
59            return false;
60        };
61        if record.filter == filter {
62            return true;
63        }
64        record.filter = filter;
65        self.wake(id);
66        if let Some(pipeline) = self.contacts.as_mut() {
67            pipeline.destroy_filtered_contacts_of(id, &self.records);
68            if let Some(record) = self.records.get(id) {
69                pipeline.rediscover(&record.proxies);
70            }
71        }
72        true
73    }
74
75    /// `entity`'s collision filter, if it is registered.
76    #[must_use]
77    pub fn collision_filter(&self, entity: Entity) -> Option<CollisionFilter> {
78        self.record(entity).map(|record| record.filter)
79    }
80}