bevy_boxddd 0.2.0

Bevy integration for the boxddd Rust bindings to Box3D
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Debug draw collection and optional Bevy gizmo rendering.

use crate::errors::report_error;
#[cfg(feature = "debug-gizmos")]
use crate::math::{to_bevy_pos, to_bevy_quat, to_bevy_transform, to_bevy_vec3};
use crate::messages::{BoxdddErrorMessage, BoxdddOperation};
use crate::resources::{BoxdddPhysicsContext, BoxdddPhysicsSettings};
use bevy_ecs::message::MessageWriter;
use bevy_ecs::prelude::{NonSendMut, Res, ResMut, Resource};
use std::collections::HashMap;

/// Controls whether Box3D debug draw commands are collected after each step.
#[derive(Resource, Copy, Clone, Debug)]
pub struct BoxdddDebugDrawSettings {
    /// Enables debug draw command collection when true.
    pub enabled: bool,
    /// Box3D debug draw options forwarded to the native world.
    pub options: boxddd::DebugDrawOptions,
}

impl Default for BoxdddDebugDrawSettings {
    fn default() -> Self {
        Self {
            enabled: false,
            options: boxddd::DebugDrawOptions::default(),
        }
    }
}

/// Last collected Box3D debug draw frame and persistent shape asset cache.
#[derive(Resource, Clone, Debug, Default)]
pub struct BoxdddDebugDrawFrame {
    frame: boxddd::DebugDrawFrame,
    assets: HashMap<boxddd::DebugShapeHandle, boxddd::DebugShapeAsset>,
}

impl BoxdddDebugDrawFrame {
    /// Returns the complete frame collected during the most recent debug draw pass.
    pub fn frame(&self) -> &boxddd::DebugDrawFrame {
        &self.frame
    }

    /// Returns debug shape lifecycle events collected during the most recent pass.
    pub fn events(&self) -> &[boxddd::DebugShapeEvent] {
        &self.frame.events
    }

    /// Returns the commands collected during the most recent debug draw pass.
    pub fn commands(&self) -> &[boxddd::DebugDrawCommand] {
        &self.frame.commands
    }

    /// Returns non-fatal diagnostics collected while building the frame.
    pub fn diagnostics(&self) -> &[boxddd::DebugDrawDiagnostic] {
        &self.frame.diagnostics
    }

    /// Returns persistent debug shape assets keyed by handle.
    pub fn assets(&self) -> &HashMap<boxddd::DebugShapeHandle, boxddd::DebugShapeAsset> {
        &self.assets
    }

    /// Returns one cached debug shape asset.
    pub fn asset(&self, handle: boxddd::DebugShapeHandle) -> Option<&boxddd::DebugShapeAsset> {
        self.assets.get(&handle)
    }

    /// Clears the most recent frame while preserving cached debug shape assets.
    pub fn clear(&mut self) {
        self.frame.clear();
    }

    /// Clears cached debug shape assets without touching the most recent frame.
    ///
    /// This is useful when a renderer rebuilds all of its GPU resources.
    pub fn clear_cached_assets(&mut self) {
        self.assets.clear();
    }

    /// Clears both the current frame and all cached debug shape assets.
    pub fn clear_all(&mut self) {
        self.clear();
        self.clear_cached_assets();
    }

    fn apply_events(&mut self) {
        for event in &self.frame.events {
            match event {
                boxddd::DebugShapeEvent::Created(asset) => {
                    self.assets.insert(asset.handle, asset.clone());
                }
                boxddd::DebugShapeEvent::Destroyed { handle } => {
                    self.assets.remove(handle);
                }
                boxddd::DebugShapeEvent::ClearAll => {
                    self.assets.clear();
                }
            }
        }
    }

    fn record_missing_asset_diagnostics(&mut self) {
        let missing = self
            .frame
            .commands
            .iter()
            .filter_map(|command| match command {
                boxddd::DebugDrawCommand::Shape {
                    handle: Some(handle),
                    ..
                } if !self.assets.contains_key(handle) => Some(*handle),
                _ => None,
            })
            .fold(Vec::new(), |mut handles, handle| {
                if !handles.contains(&handle) {
                    handles.push(handle);
                }
                handles
            });

        for handle in missing {
            self.frame.diagnostics.push(boxddd::DebugDrawDiagnostic {
                message: format!(
                    "debug shape asset missing for handle {}:{}",
                    handle.index, handle.generation
                ),
            });
        }
    }
}

/// Backwards-compatible name for the Bevy debug draw frame resource.
pub type BoxdddDebugDrawCommands = BoxdddDebugDrawFrame;

/// Collects native Box3D debug draw data into [`BoxdddDebugDrawFrame`].
pub fn collect_debug_draw_commands(
    mut context: NonSendMut<BoxdddPhysicsContext>,
    settings: Res<BoxdddPhysicsSettings>,
    debug_settings: Res<BoxdddDebugDrawSettings>,
    mut debug_frame: ResMut<BoxdddDebugDrawFrame>,
    mut errors: MessageWriter<BoxdddErrorMessage>,
) {
    if !debug_settings.enabled {
        debug_frame.clear();
        return;
    }

    let Some(world) = context.world_mut() else {
        debug_frame.clear_all();
        return;
    };

    let result = world.try_debug_draw_frame_into(&mut debug_frame.frame, debug_settings.options);

    if let Err(error) = result {
        debug_frame.clear();
        report_error(
            &settings,
            &mut errors,
            BoxdddErrorMessage {
                operation: BoxdddOperation::DebugDraw,
                entity: None,
                error,
            },
        );
    } else {
        debug_frame.apply_events();
        debug_frame.record_missing_asset_diagnostics();
    }
}

#[cfg(feature = "debug-gizmos")]
/// Renders collected Box3D debug draw commands using Bevy `Gizmos`.
pub fn draw_debug_gizmos(
    debug_frame: Res<BoxdddDebugDrawFrame>,
    mut gizmos: bevy_gizmos::prelude::Gizmos,
) {
    for command in debug_frame.commands() {
        draw_debug_command(&mut gizmos, &debug_frame, command);
    }
}

#[cfg(feature = "debug-gizmos")]
fn draw_debug_command(
    gizmos: &mut bevy_gizmos::prelude::Gizmos,
    debug_frame: &BoxdddDebugDrawFrame,
    command: &boxddd::DebugDrawCommand,
) {
    match command {
        boxddd::DebugDrawCommand::Shape {
            handle: Some(handle),
            transform,
            color,
        } => {
            if let Some(asset) = debug_frame.asset(*handle) {
                draw_debug_shape_geometry(
                    gizmos,
                    &asset.geometry,
                    to_bevy_pos(transform.p),
                    to_bevy_quat(transform.q),
                    to_bevy_color(*color),
                );
            }
        }
        boxddd::DebugDrawCommand::Shape { handle: None, .. } => {}
        boxddd::DebugDrawCommand::Segment { p1, p2, color } => {
            gizmos.line(to_bevy_pos(*p1), to_bevy_pos(*p2), to_bevy_color(*color));
        }
        boxddd::DebugDrawCommand::Transform(transform) => {
            gizmos.axes(to_bevy_transform(*transform), 0.45);
        }
        boxddd::DebugDrawCommand::Point {
            position,
            size,
            color,
        } => {
            gizmos.sphere(
                to_bevy_pos(*position),
                (*size).max(1.0) * 0.01,
                to_bevy_color(*color),
            );
        }
        boxddd::DebugDrawCommand::Sphere {
            center,
            radius,
            color,
            ..
        } => {
            gizmos.sphere(to_bevy_pos(*center), *radius, to_bevy_color(*color));
        }
        boxddd::DebugDrawCommand::Capsule {
            p1,
            p2,
            radius,
            color,
            ..
        } => {
            let p1 = to_bevy_pos(*p1);
            let p2 = to_bevy_pos(*p2);
            let color = to_bevy_color(*color);
            gizmos.line(p1, p2, color);
            gizmos.sphere(p1, *radius, color);
            gizmos.sphere(p2, *radius, color);
        }
        boxddd::DebugDrawCommand::Bounds { aabb, color } => {
            let lower = to_bevy_vec3(aabb.lower_bound);
            let upper = to_bevy_vec3(aabb.upper_bound);
            let center = (lower + upper) * 0.5;
            let scale = upper - lower;
            gizmos.cube(
                bevy_transform::components::Transform::from_translation(center).with_scale(scale),
                to_bevy_color(*color),
            );
        }
        boxddd::DebugDrawCommand::Box {
            extents,
            transform,
            color,
        } => {
            gizmos.cube(
                to_bevy_transform(*transform).with_scale(to_bevy_vec3(*extents) * 2.0),
                to_bevy_color(*color),
            );
        }
        boxddd::DebugDrawCommand::String {
            position,
            text,
            color,
        } => {
            gizmos.text(
                bevy_math::Isometry3d::from_translation(to_bevy_pos(*position)),
                text,
                14.0,
                bevy_math::Vec2::ZERO,
                to_bevy_color(*color),
            );
        }
    }
}

#[cfg(feature = "debug-gizmos")]
fn draw_debug_shape_geometry(
    gizmos: &mut bevy_gizmos::prelude::Gizmos,
    geometry: &boxddd::DebugShapeGeometry,
    origin: bevy_math::Vec3,
    rotation: bevy_math::Quat,
    color: bevy_color::Color,
) {
    match geometry {
        boxddd::DebugShapeGeometry::Sphere { center, radius } => {
            gizmos.sphere(
                transform_local_point(origin, rotation, *center),
                *radius,
                color,
            );
        }
        boxddd::DebugShapeGeometry::Capsule {
            center1,
            center2,
            radius,
        } => {
            let p1 = transform_local_point(origin, rotation, *center1);
            let p2 = transform_local_point(origin, rotation, *center2);
            gizmos.line(p1, p2, color);
            gizmos.sphere(p1, *radius, color);
            gizmos.sphere(p2, *radius, color);
        }
        boxddd::DebugShapeGeometry::Hull { points, faces, .. } => {
            for face in faces {
                draw_indexed_polyline(gizmos, points, &face.indices, origin, rotation, color);
            }
        }
        boxddd::DebugShapeGeometry::Mesh { mesh, scale } => {
            draw_debug_mesh(gizmos, mesh, origin, rotation, to_bevy_vec3(*scale), color);
        }
        boxddd::DebugShapeGeometry::HeightField { mesh } => {
            draw_debug_mesh(gizmos, mesh, origin, rotation, bevy_math::Vec3::ONE, color);
        }
        boxddd::DebugShapeGeometry::Compound { children } => {
            for child in children {
                let child_origin = transform_local_point(origin, rotation, child.transform.p);
                let child_rotation = rotation * to_bevy_quat(child.transform.q);
                draw_debug_shape_geometry(
                    gizmos,
                    &child.geometry,
                    child_origin,
                    child_rotation,
                    color,
                );
            }
        }
    }
}

#[cfg(feature = "debug-gizmos")]
fn draw_debug_mesh(
    gizmos: &mut bevy_gizmos::prelude::Gizmos,
    mesh: &boxddd::DebugMesh,
    origin: bevy_math::Vec3,
    rotation: bevy_math::Quat,
    scale: bevy_math::Vec3,
    color: bevy_color::Color,
) {
    for triangle in &mesh.triangles {
        let Some(a) = mesh.vertices.get(triangle.indices[0] as usize) else {
            continue;
        };
        let Some(b) = mesh.vertices.get(triangle.indices[1] as usize) else {
            continue;
        };
        let Some(c) = mesh.vertices.get(triangle.indices[2] as usize) else {
            continue;
        };
        let a = transform_local_scaled_point(origin, rotation, *a, scale);
        let b = transform_local_scaled_point(origin, rotation, *b, scale);
        let c = transform_local_scaled_point(origin, rotation, *c, scale);
        gizmos.line(a, b, color);
        gizmos.line(b, c, color);
        gizmos.line(c, a, color);
    }
}

#[cfg(feature = "debug-gizmos")]
fn draw_indexed_polyline(
    gizmos: &mut bevy_gizmos::prelude::Gizmos,
    points: &[boxddd::Vec3],
    indices: &[u32],
    origin: bevy_math::Vec3,
    rotation: bevy_math::Quat,
    color: bevy_color::Color,
) {
    let mut vertices = indices
        .iter()
        .filter_map(|index| points.get(*index as usize))
        .copied()
        .map(|point| transform_local_point(origin, rotation, point));
    let Some(first) = vertices.next() else {
        return;
    };
    let mut previous = first;
    for current in vertices {
        gizmos.line(previous, current, color);
        previous = current;
    }
    gizmos.line(previous, first, color);
}

#[cfg(feature = "debug-gizmos")]
fn transform_local_point(
    origin: bevy_math::Vec3,
    rotation: bevy_math::Quat,
    point: boxddd::Vec3,
) -> bevy_math::Vec3 {
    origin + rotation * to_bevy_vec3(point)
}

#[cfg(feature = "debug-gizmos")]
fn transform_local_scaled_point(
    origin: bevy_math::Vec3,
    rotation: bevy_math::Quat,
    point: boxddd::Vec3,
    scale: bevy_math::Vec3,
) -> bevy_math::Vec3 {
    origin + rotation * (to_bevy_vec3(point) * scale)
}

#[cfg(feature = "debug-gizmos")]
fn to_bevy_color(color: boxddd::HexColor) -> bevy_color::Color {
    let rgb = color.rgb_u32();
    let red = ((rgb >> 16) & 0xff) as f32 / 255.0;
    let green = ((rgb >> 8) & 0xff) as f32 / 255.0;
    let blue = (rgb & 0xff) as f32 / 255.0;
    bevy_color::Color::srgb(red, green, blue)
}