aboutsummaryrefslogtreecommitdiff
path: root/src_testbed
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2022-04-22 15:45:53 +0200
committerSébastien Crozet <developer@crozet.re>2022-04-22 16:11:23 +0200
commitbc2ae4b512b8bc7a2b61dd24d9685289453681c5 (patch)
tree576d950ee18b0a3a2e69a48db5fd2ba14f7bcd39 /src_testbed
parent21a31bc1026d17d30b3a5ac35e6bb716dc66be6e (diff)
downloadrapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.tar.gz
rapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.tar.bz2
rapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.zip
Add a basic lines-based debug-renderer
Diffstat (limited to 'src_testbed')
-rw-r--r--src_testbed/debug_render.rs81
-rw-r--r--src_testbed/lib.rs1
-rw-r--r--src_testbed/testbed.rs7
3 files changed, 86 insertions, 3 deletions
diff --git a/src_testbed/debug_render.rs b/src_testbed/debug_render.rs
new file mode 100644
index 0000000..135529f
--- /dev/null
+++ b/src_testbed/debug_render.rs
@@ -0,0 +1,81 @@
+use crate::harness::Harness;
+use bevy::prelude::*;
+use bevy_prototype_debug_lines::DebugLines;
+use rapier::math::{Point, Real, DIM};
+use rapier::pipeline::{
+ DebugRenderBackend, DebugRenderMode, DebugRenderObject, DebugRenderPipeline,
+};
+use std::fmt::Debug;
+
+pub struct RapierDebugRenderPlugin {
+ depth_test: bool,
+}
+
+impl Default for RapierDebugRenderPlugin {
+ fn default() -> Self {
+ Self {
+ depth_test: cfg!(feature = "dim3"),
+ }
+ }
+}
+
+impl RapierDebugRenderPlugin {
+ pub fn with_depth_test(enabled: bool) -> Self {
+ Self {
+ depth_test: enabled,
+ }
+ }
+}
+
+impl Plugin for RapierDebugRenderPlugin {
+ fn build(&self, app: &mut App) {
+ app.add_plugin(
+ bevy_prototype_debug_lines::DebugLinesPlugin::with_depth_test(self.depth_test),
+ )
+ .insert_resource(DebugRenderPipeline::new(
+ Default::default(),
+ !DebugRenderMode::RIGID_BODY_AXES,
+ ))
+ .add_system_to_stage(CoreStage::Update, debug_render_scene);
+ }
+}
+
+struct BevyLinesRenderBackend<'a> {
+ lines: &'a mut DebugLines,
+}
+
+impl<'a> DebugRenderBackend for BevyLinesRenderBackend<'a> {
+ #[cfg(feature = "dim2")]
+ fn draw_line(&mut self, _: DebugRenderObject, a: Point<Real>, b: Point<Real>, color: [f32; 4]) {
+ self.lines.line_colored(
+ [a.x, a.y, 1.0e-8].into(),
+ [b.x, b.y, 1.0e-8].into(),
+ 0.0,
+ Color::hsla(color[0], color[1], color[2], color[3]),
+ )
+ }
+ #[cfg(feature = "dim3")]
+ fn draw_line(&mut self, _: DebugRenderObject, a: Point<Real>, b: Point<Real>, color: [f32; 4]) {
+ self.lines.line_colored(
+ [a.x, a.y, a.z].into(),
+ [b.x, b.y, b.z].into(),
+ 0.0,
+ Color::hsla(color[0], color[1], color[2], color[3]),
+ )
+ }
+}
+
+fn debug_render_scene(
+ mut pipeline: ResMut<DebugRenderPipeline>,
+ harness: NonSend<Harness>,
+ mut lines: ResMut<DebugLines>,
+) {
+ let mut backend = BevyLinesRenderBackend { lines: &mut *lines };
+ pipeline.render(
+ &mut backend,
+ &harness.physics.bodies,
+ &harness.physics.colliders,
+ &harness.physics.impulse_joints,
+ &harness.physics.multibody_joints,
+ );
+}
diff --git a/src_testbed/lib.rs b/src_testbed/lib.rs
index bbf2d2f..881e05d 100644
--- a/src_testbed/lib.rs
+++ b/src_testbed/lib.rs
@@ -19,6 +19,7 @@ mod box2d_backend;
mod camera2d;
#[cfg(feature = "dim3")]
mod camera3d;
+mod debug_render;
mod graphics;
pub mod harness;
pub mod objects;
diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs
index 1bb30eb..2c40cf2 100644
--- a/src_testbed/testbed.rs
+++ b/src_testbed/testbed.rs
@@ -5,7 +5,7 @@ use bevy::prelude::*;
use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState};
use crate::plugin::TestbedPlugin;
-use crate::ui;
+use crate::{debug_render, ui};
use crate::{graphics::GraphicsManager, harness::RunState};
use na::{self, Point2, Point3, Vector3};
@@ -17,7 +17,7 @@ use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase};
#[cfg(feature = "dim3")]
use rapier::geometry::{InteractionGroups, Ray};
use rapier::math::{Real, Vector};
-use rapier::pipeline::PhysicsHooks;
+use rapier::pipeline::{DebugRenderMode, PhysicsHooks};
#[cfg(all(feature = "dim2", feature = "other-backends"))]
use crate::box2d_backend::Box2dWorld;
@@ -380,7 +380,8 @@ impl TestbedApp {
.add_plugins(DefaultPlugins)
.add_plugin(OrbitCameraPlugin)
.add_plugin(WireframePlugin)
- .add_plugin(bevy_egui::EguiPlugin);
+ .add_plugin(bevy_egui::EguiPlugin)
+ .add_plugin(debug_render::RapierDebugRenderPlugin::default());
#[cfg(target_arch = "wasm32")]
app.add_plugin(bevy_webgl2::WebGL2Plugin);