ncollide3d/pipeline/narrow_phase/
events.rs

1//! Structures for describing and storing collision-related events.
2
3use crate::query::Proximity;
4use std::iter::IntoIterator;
5use std::slice::Iter;
6
7// FIXME: we want a structure where we can add elements, iterate on them, but not remove them
8// without clearing the whole structure.
9/// A set of events.
10pub struct EventPool<E> {
11    events: Vec<E>,
12}
13
14/// A set of contact events.
15pub type ContactEvents<Handle> = EventPool<ContactEvent<Handle>>;
16/// A set of proximity events.
17pub type ProximityEvents<Handle> = EventPool<ProximityEvent<Handle>>;
18
19impl<E> EventPool<E> {
20    /// Creates a new empty set of events.
21    pub fn new() -> EventPool<E> {
22        EventPool { events: Vec::new() }
23    }
24
25    /// Empties this set of events.
26    pub fn clear(&mut self) {
27        self.events.clear();
28    }
29
30    /// Adds the given event at the end of this set.
31    pub fn push(&mut self, event: E) {
32        self.events.push(event);
33    }
34
35    /// Iterates through all events contained on this set in a FIFO manner.
36    pub fn iter(&self) -> Iter<E> {
37        self.events.iter()
38    }
39
40    /// Removes from this set all events for which `filter` returns `false`.
41    pub fn retain<F>(&mut self, filter: F)
42    where
43        F: FnMut(&E) -> bool,
44    {
45        self.events.retain(filter)
46    }
47
48    /// The number of events on this pool.
49    #[inline]
50    pub fn len(&self) -> usize {
51        self.events.len()
52    }
53}
54
55impl<'a, E> IntoIterator for &'a EventPool<E> {
56    type Item = &'a E;
57    type IntoIter = Iter<'a, E>;
58
59    fn into_iter(self) -> Iter<'a, E> {
60        (&self.events).into_iter()
61    }
62}
63
64#[derive(Copy, Clone, Hash, Debug)]
65/// Events occuring when two collision objects start or stop being in contact (or penetration).
66pub enum ContactEvent<Handle> {
67    /// Event occuring when two collision objects start being in contact.
68    ///
69    /// This event is generated whenever the narrow-phase finds a contact between two collision objects that did not have any contact at the last update.
70    Started(Handle, Handle),
71    /// Event occuring when two collision objects stop being in contact.
72    ///
73    /// This event is generated whenever the narrow-phase fails to find any contact between two collision objects that did have at least one contact at the last update.
74    Stopped(Handle, Handle),
75}
76
77#[derive(Copy, Clone, Debug)]
78/// Events occuring when two collision objects start or stop being in close proximity, contact, or disjoint.
79pub struct ProximityEvent<Handle> {
80    /// The first collider to which the proximity event applies.
81    pub collider1: Handle,
82    /// The second collider to which the proximity event applies.
83    pub collider2: Handle,
84    /// The previous state of proximity between the two collision objects.
85    pub prev_status: Proximity,
86    /// The new state of proximity between the two collision objects.
87    pub new_status: Proximity,
88}
89
90impl<Handle> ProximityEvent<Handle> {
91    /// Instantiates a new proximity event.
92    ///
93    /// Panics if `prev_status` is equal to `new_status`.
94    pub fn new(
95        collider1: Handle,
96        collider2: Handle,
97        prev_status: Proximity,
98        new_status: Proximity,
99    ) -> Self {
100        assert_ne!(
101            prev_status, new_status,
102            "The previous and new status of a proximity event must not be the same."
103        );
104        Self {
105            collider1,
106            collider2,
107            prev_status,
108            new_status,
109        }
110    }
111}