crcbl_phys/system/ignored_contacts.rs
1//! [`PhysicsSystem`]'s ignored pairs: two bodies that never collide with each
2//! other, though no joint joins them. See `crate::contact::ignored`.
3
4use crcbl_ecs::Entity;
5
6use super::{BodyId, PhysicsSystem};
7
8/// Why [`PhysicsSystem::ignore_contacts`] or
9/// [`PhysicsSystem::restore_contacts`] refused a pair.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum ContactPairError {
12 /// An entity that is not registered with the system.
13 UnknownBody(Entity),
14 /// Both sides are one body.
15 SameBody,
16}
17
18impl core::fmt::Display for ContactPairError {
19 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20 match self {
21 Self::UnknownBody(entity) => write!(f, "{entity:?} is not registered"),
22 Self::SameBody => f.write_str("a contact pair is two different bodies"),
23 }
24 }
25}
26
27impl std::error::Error for ContactPairError {}
28
29impl PhysicsSystem {
30 /// Stops the bodies of `a` and `b` colliding with each other, and wakes
31 /// both: every contact between them is ended, as a joint that keeps its
32 /// bodies apart ends theirs. Each still collides with everything else.
33 /// Returns `Ok(false)` if the pair was already ignored.
34 ///
35 /// This is for bodies that overlap by design without a joint between
36 /// them, such as two limbs of one ragdoll meeting at its pelvis. The pair
37 /// is forgotten when either entity is removed.
38 ///
39 /// # Errors
40 ///
41 /// [`ContactPairError::UnknownBody`] if either entity is not registered,
42 /// and [`ContactPairError::SameBody`] if the two are one.
43 ///
44 /// # Panics
45 ///
46 /// Panics if this system has no contacts. Build it
47 /// [`with_contacts`](Self::with_contacts).
48 pub fn ignore_contacts(&mut self, a: Entity, b: Entity) -> Result<bool, ContactPairError> {
49 let (a, b) = self.contact_pair(a, b)?;
50 self.wake(a);
51 self.wake(b);
52 let pipeline = self.contacts.as_mut().expect("checked by contact_pair");
53 let added = pipeline.ignored.insert(a, b);
54 if added {
55 pipeline.destroy_contacts_between(a, b);
56 }
57 Ok(added)
58 }
59
60 /// Lets the bodies of `a` and `b` collide with each other again, unless a
61 /// joint still keeps them apart, and wakes both. Returns `Ok(false)` if
62 /// the pair was not ignored.
63 ///
64 /// # Errors
65 ///
66 /// As [`ignore_contacts`](Self::ignore_contacts)'s.
67 ///
68 /// # Panics
69 ///
70 /// Panics if this system has no contacts.
71 pub fn restore_contacts(&mut self, a: Entity, b: Entity) -> Result<bool, ContactPairError> {
72 let (a, b) = self.contact_pair(a, b)?;
73 self.wake(a);
74 self.wake(b);
75 let removed = self
76 .contacts
77 .as_mut()
78 .expect("checked by contact_pair")
79 .ignored
80 .remove(a, b);
81 if removed {
82 self.let_collide(a, b);
83 }
84 Ok(removed)
85 }
86
87 /// Whether [`ignore_contacts`](Self::ignore_contacts) keeps the bodies of
88 /// `a` and `b` apart. A joint's own rule is not counted here.
89 #[must_use]
90 pub fn ignores_contacts(&self, a: Entity, b: Entity) -> bool {
91 let (Some(pipeline), Some(&a), Some(&b)) = (
92 self.contacts.as_ref(),
93 self.entity_to_body.get(&a),
94 self.entity_to_body.get(&b),
95 ) else {
96 return false;
97 };
98 pipeline.ignored.contains(a, b)
99 }
100
101 /// Both bodies of a pair, if it can be ignored.
102 fn contact_pair(&self, a: Entity, b: Entity) -> Result<(BodyId, BodyId), ContactPairError> {
103 assert!(
104 self.contacts.is_some(),
105 "ignored pairs are a contact-pipeline rule: build the system with_contacts"
106 );
107 let id = |entity: Entity| {
108 self.entity_to_body
109 .get(&entity)
110 .copied()
111 .ok_or(ContactPairError::UnknownBody(entity))
112 };
113 let (a, b) = (id(a)?, id(b)?);
114 if a == b {
115 return Err(ContactPairError::SameBody);
116 }
117 Ok((a, b))
118 }
119}