Skip to main content

oxihuman_viewer/
physics_debug_view.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Physics simulation debug overlay — master toggle for all physics debug layers.
6
7/// Physics debug view configuration.
8#[derive(Debug, Clone)]
9pub struct PhysicsDebugView {
10    pub enabled: bool,
11    pub show_colliders: bool,
12    pub show_forces: bool,
13    pub show_joints: bool,
14    pub opacity: f32,
15}
16
17impl PhysicsDebugView {
18    pub fn new() -> Self {
19        Self {
20            enabled: false,
21            show_colliders: true,
22            show_forces: true,
23            show_joints: true,
24            opacity: 0.8,
25        }
26    }
27}
28
29impl Default for PhysicsDebugView {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35/// Create a new physics debug view.
36pub fn new_physics_debug_view() -> PhysicsDebugView {
37    PhysicsDebugView::new()
38}
39
40/// Enable or disable the physics debug overlay.
41pub fn pdv_set_enabled(v: &mut PhysicsDebugView, enabled: bool) {
42    v.enabled = enabled;
43}
44
45/// Toggle collider wireframes.
46pub fn pdv_set_show_colliders(v: &mut PhysicsDebugView, show: bool) {
47    v.show_colliders = show;
48}
49
50/// Toggle force vector display.
51pub fn pdv_set_show_forces(v: &mut PhysicsDebugView, show: bool) {
52    v.show_forces = show;
53}
54
55/// Set overlay opacity.
56pub fn pdv_set_opacity(v: &mut PhysicsDebugView, opacity: f32) {
57    v.opacity = opacity.clamp(0.0, 1.0);
58}
59
60/// Serialize to JSON-like string.
61pub fn physics_debug_view_to_json(v: &PhysicsDebugView) -> String {
62    format!(
63        r#"{{"enabled":{},"show_colliders":{},"show_forces":{},"opacity":{:.4}}}"#,
64        v.enabled, v.show_colliders, v.show_forces, v.opacity
65    )
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_defaults() {
74        let v = new_physics_debug_view();
75        assert!(!v.enabled);
76        assert!(v.show_colliders);
77    }
78
79    #[test]
80    fn test_enable() {
81        let mut v = new_physics_debug_view();
82        pdv_set_enabled(&mut v, true);
83        assert!(v.enabled);
84    }
85
86    #[test]
87    fn test_disable_colliders() {
88        let mut v = new_physics_debug_view();
89        pdv_set_show_colliders(&mut v, false);
90        assert!(!v.show_colliders);
91    }
92
93    #[test]
94    fn test_forces_toggle() {
95        let mut v = new_physics_debug_view();
96        pdv_set_show_forces(&mut v, false);
97        assert!(!v.show_forces);
98    }
99
100    #[test]
101    fn test_opacity_clamp() {
102        let mut v = new_physics_debug_view();
103        pdv_set_opacity(&mut v, 2.0);
104        assert_eq!(v.opacity, 1.0);
105    }
106
107    #[test]
108    fn test_opacity_set() {
109        let mut v = new_physics_debug_view();
110        pdv_set_opacity(&mut v, 0.5);
111        assert!((v.opacity - 0.5).abs() < 1e-6);
112    }
113
114    #[test]
115    fn test_json_keys() {
116        let v = new_physics_debug_view();
117        let s = physics_debug_view_to_json(&v);
118        assert!(s.contains("show_colliders"));
119    }
120
121    #[test]
122    fn test_clone() {
123        let v = new_physics_debug_view();
124        let v2 = v.clone();
125        assert_eq!(v2.enabled, v.enabled);
126    }
127
128    #[test]
129    fn test_default_opacity() {
130        let v = new_physics_debug_view();
131        assert!((v.opacity - 0.8).abs() < 1e-6);
132    }
133}