Skip to main content

avian3d/collision/
diagnostics.rs

1use bevy::{
2    diagnostic::DiagnosticPath,
3    prelude::{ReflectResource, Resource},
4    reflect::Reflect,
5};
6use core::time::Duration;
7
8use crate::diagnostics::{PhysicsDiagnostics, impl_diagnostic_paths};
9
10/// Diagnostics for collision detection.
11#[derive(Resource, Debug, Default, Reflect)]
12#[reflect(Resource, Debug)]
13pub struct CollisionDiagnostics {
14    /// Time spent finding potential collision pairs in the [broad phase](crate::collision::broad_phase).
15    pub broad_phase: Duration,
16    /// Time spent updating contacts in the [narrow phase](crate::collision::narrow_phase).
17    pub narrow_phase: Duration,
18    /// The number of contacts.
19    pub contact_count: u32,
20    /// The number of contacts that were recycled from the previous timestep.
21    pub recycled_count: u32,
22}
23
24impl PhysicsDiagnostics for CollisionDiagnostics {
25    fn timer_paths(&self) -> Vec<(&'static DiagnosticPath, Duration)> {
26        vec![
27            (Self::BROAD_PHASE, self.broad_phase),
28            (Self::NARROW_PHASE, self.narrow_phase),
29        ]
30    }
31
32    fn counter_paths(&self) -> Vec<(&'static DiagnosticPath, u32)> {
33        vec![
34            (Self::CONTACT_COUNT, self.contact_count),
35            (Self::RECYCLED_COUNT, self.recycled_count),
36        ]
37    }
38}
39
40impl_diagnostic_paths! {
41    impl CollisionDiagnostics {
42        BROAD_PHASE: "avian/collision/broad_phase",
43        NARROW_PHASE: "avian/collision/update_contacts",
44        CONTACT_COUNT: "avian/collision/contact_count",
45        RECYCLED_COUNT: "avian/collision/recycled_count",
46    }
47}