Last active
January 23, 2025 13:17
-
-
Save RJ/b1853594ab4dabf6fff3e1d6ab2888df to your computer and use it in GitHub Desktop.
Easy toggler for Avian's PhysicsDebugPlugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use avian2d::prelude::{PhysicsDebugPlugin, PhysicsGizmos}; | |
use bevy::{input::common_conditions::input_just_pressed, prelude::*}; | |
pub mod prelude { | |
pub use super::PhysicsDebugToggle; | |
} | |
/// Controls if we run avians physicsdebug plugin to draw gizmos. | |
/// Field has a nice name so it shows up better in the egui resource inspector. | |
#[derive(Resource, Reflect, Default)] | |
pub struct PhysicsDebugToggle { | |
pub draw_physics_debug: bool, | |
} | |
pub fn plugin(app: &mut App) { | |
app.add_plugins(PhysicsDebugPlugin::default()); | |
app.init_resource::<PhysicsDebugToggle>(); | |
app.add_systems( | |
Update, | |
( | |
physics_debug_res_changed.run_if(resource_changed::<PhysicsDebugToggle>), | |
toggle_physics_debug.run_if(input_just_pressed(KeyCode::F11)), | |
), | |
); | |
} | |
fn toggle_physics_debug(mut res: ResMut<PhysicsDebugToggle>) { | |
res.draw_physics_debug = !res.draw_physics_debug; | |
} | |
fn physics_debug_res_changed( | |
toggle: ResMut<PhysicsDebugToggle>, | |
mut gcs: ResMut<GizmoConfigStore>, | |
) { | |
let gc = gcs.config_mut::<PhysicsGizmos>().0; | |
gc.enabled = toggle.draw_physics_debug; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment