use crate::_bevy::*;
use crate::components::{AeroZone, AircraftGeometry, EngineZone};
use avian3d::prelude::Collider;
use bevy_transform::TransformSystems;
use super::configuration::FdmGizmos;
#[derive(Resource, Default, Debug)]
pub struct ShowColliders(pub bool);
const COLLIDER_COLOR: bevy_color::Color = bevy_color::Color::srgba(0.8, 0.5, 0.2, 0.8);
pub(super) fn plugin(app: &mut App) {
app.init_resource::<ShowColliders>();
app.add_systems(
PostUpdate,
debug_render_colliders
.after(TransformSystems::Propagate)
.run_if(|show: Res<ShowColliders>, store: Res<GizmoConfigStore>| {
show.0 && store.config::<FdmGizmos>().0.enabled
}),
);
}
#[allow(clippy::unnecessary_cast)]
#[allow(clippy::type_complexity)]
pub(super) fn debug_render_colliders(
mut gizmos: Gizmos<FdmGizmos>,
root_query: Query<&Transform, With<AircraftGeometry>>,
zone_query: Query<(&Transform, Option<&Collider>), Or<(With<AeroZone>, With<EngineZone>)>>,
) {
use avian3d::parry::shape::TypedShape;
let Ok(root_tf) = root_query.single() else {
return;
};
let t = root_tf.translation;
let r = root_tf.rotation;
let iso_at = |zone_tf: &Transform| {
Isometry3d::new(
t + r * zone_tf.translation,
Quat::from_array(r.to_array()) * Quat::from_array(zone_tf.rotation.to_array()),
)
};
for (zone_tf, collider) in &zone_query {
let Some(col) = collider else { continue };
let iso = iso_at(zone_tf);
match col.shape_scaled().as_typed_shape() {
TypedShape::Cuboid(c) => {
let he = c.half_extents;
gizmos.primitive_3d(
&Cuboid::new(he.x as f32 * 2.0, he.y as f32 * 2.0, he.z as f32 * 2.0),
iso,
COLLIDER_COLOR,
);
}
TypedShape::Ball(b) => {
gizmos
.primitive_3d(&Sphere::new(b.radius as f32), iso, COLLIDER_COLOR)
.resolution(32);
}
TypedShape::Cylinder(c) => {
gizmos
.primitive_3d(
&Cylinder::new(c.radius as f32, c.half_height as f32 * 2.0),
iso,
COLLIDER_COLOR,
)
.resolution(32);
}
TypedShape::Capsule(c) => {
gizmos.primitive_3d(
&Capsule3d::new(c.radius as f32, c.half_height() as f32 * 2.0),
iso,
COLLIDER_COLOR,
);
}
_ => {}
}
}
}