Skip to main content

rapier2d/counters/
collision_detection_counters.rs

1use crate::counters::Timer;
2use std::fmt::{Display, Formatter, Result};
3
4/// Performance counters related to collision detection.
5#[derive(Default, Clone, Copy)]
6pub struct CollisionDetectionCounters {
7    /// Number of contact pairs detected.
8    pub ncontact_pairs: usize,
9    /// Time spent for the broad-phase of the collision detection.
10    pub broad_phase_time: Timer,
11    /// Time spent by the final broad-phase AABB update after body movement to keep
12    /// user scene queries valid.
13    pub final_broad_phase_time: Timer,
14    /// Time spent for the narrow-phase of the collision detection.
15    pub narrow_phase_time: Timer,
16}
17
18impl CollisionDetectionCounters {
19    /// Creates a new counter initialized to zero.
20    pub fn new() -> Self {
21        CollisionDetectionCounters {
22            ncontact_pairs: 0,
23            broad_phase_time: Timer::new(),
24            final_broad_phase_time: Timer::new(),
25            narrow_phase_time: Timer::new(),
26        }
27    }
28
29    /// Resets all the counters and timers.
30    pub fn reset(&mut self) {
31        self.ncontact_pairs = 0;
32        self.broad_phase_time.reset();
33        self.final_broad_phase_time.reset();
34        self.narrow_phase_time.reset();
35    }
36}
37
38impl Display for CollisionDetectionCounters {
39    fn fmt(&self, f: &mut Formatter) -> Result {
40        writeln!(f, "Number of contact pairs: {}", self.ncontact_pairs)?;
41        writeln!(f, "Broad-phase time: {}", self.broad_phase_time)?;
42        writeln!(f, "Final broad-phase time: {}", self.final_broad_phase_time)?;
43        writeln!(f, "Narrow-phase time: {}", self.narrow_phase_time)
44    }
45}