use crate::prelude::*;
use bevy::{color::palettes::css::*, prelude::*};
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
#[cfg_attr(
feature = "2d",
doc = " commands.spawn((RigidBody::Dynamic, Collider::circle(0.5)));"
)]
#[cfg_attr(
feature = "3d",
doc = " commands.spawn((RigidBody::Dynamic, Collider::sphere(0.5)));"
)]
#[derive(Reflect, GizmoConfigGroup)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
pub struct PhysicsGizmos {
pub axis_lengths: Option<Vector>,
pub aabb_color: Option<Color>,
pub collider_color: Option<Color>,
pub sleeping_color_multiplier: Option<[f32; 4]>,
pub contact_point_color: Option<Color>,
pub contact_normal_color: Option<Color>,
pub contact_normal_scale: ContactGizmoScale,
pub joint_anchor_color: Option<Color>,
pub joint_separation_color: Option<Color>,
pub raycast_color: Option<Color>,
pub raycast_point_color: Option<Color>,
pub raycast_normal_color: Option<Color>,
pub shapecast_color: Option<Color>,
pub shapecast_shape_color: Option<Color>,
pub shapecast_point_color: Option<Color>,
pub shapecast_normal_color: Option<Color>,
pub island_color: Option<Color>,
pub collider_tree_color: Option<Color>,
pub hide_meshes: bool,
}
impl Default for PhysicsGizmos {
fn default() -> Self {
Self {
axis_lengths: Some(Vector::splat(0.5)),
aabb_color: None,
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
contact_point_color: None,
contact_normal_color: None,
contact_normal_scale: ContactGizmoScale::default(),
joint_anchor_color: Some(PINK.into()),
joint_separation_color: Some(RED.into()),
raycast_color: Some(RED.into()),
raycast_point_color: Some(YELLOW.into()),
raycast_normal_color: Some(PINK.into()),
shapecast_color: Some(RED.into()),
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
shapecast_point_color: Some(YELLOW.into()),
shapecast_normal_color: Some(PINK.into()),
island_color: None,
collider_tree_color: None,
hide_meshes: false,
}
}
}
#[derive(Reflect, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(PartialEq)]
pub enum ContactGizmoScale {
Constant(Scalar),
Scaled(Scalar),
}
impl Default for ContactGizmoScale {
fn default() -> Self {
Self::Scaled(0.025)
}
}
impl PhysicsGizmos {
pub fn all() -> Self {
Self {
axis_lengths: Some(Vector::splat(0.5)),
aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
contact_point_color: Some(LIGHT_CYAN.into()),
contact_normal_color: Some(RED.into()),
contact_normal_scale: ContactGizmoScale::default(),
joint_anchor_color: Some(PINK.into()),
joint_separation_color: Some(RED.into()),
raycast_color: Some(RED.into()),
raycast_point_color: Some(YELLOW.into()),
raycast_normal_color: Some(PINK.into()),
shapecast_color: Some(RED.into()),
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
shapecast_point_color: Some(YELLOW.into()),
shapecast_normal_color: Some(PINK.into()),
island_color: Some(RED.into()),
collider_tree_color: Some(Color::WHITE),
hide_meshes: true,
}
}
pub fn none() -> Self {
Self {
axis_lengths: None,
aabb_color: None,
collider_color: None,
sleeping_color_multiplier: None,
contact_point_color: None,
contact_normal_color: None,
contact_normal_scale: ContactGizmoScale::default(),
joint_anchor_color: None,
joint_separation_color: None,
raycast_color: None,
raycast_point_color: None,
raycast_normal_color: None,
shapecast_color: None,
shapecast_shape_color: None,
shapecast_point_color: None,
shapecast_normal_color: None,
island_color: None,
collider_tree_color: None,
hide_meshes: false,
}
}
pub fn axes(axis_lengths: Vector) -> Self {
Self {
axis_lengths: Some(axis_lengths),
..Self::none()
}
}
pub fn aabbs(color: Color) -> Self {
Self {
aabb_color: Some(color),
..Self::none()
}
}
pub fn colliders(color: Color) -> Self {
Self {
collider_color: Some(color),
..Self::none()
}
}
pub fn contact_points(color: Color) -> Self {
Self {
contact_point_color: Some(color),
..Self::none()
}
}
pub fn contact_normals(color: Color) -> Self {
Self {
contact_normal_color: Some(color),
..Self::none()
}
}
pub fn joints(anchor_color: Option<Color>, separation_color: Option<Color>) -> Self {
Self {
joint_anchor_color: anchor_color,
joint_separation_color: separation_color,
..Self::none()
}
}
pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
self.axis_lengths = Some(axis_lengths);
self
}
pub fn with_aabb_color(mut self, color: Color) -> Self {
self.aabb_color = Some(color);
self
}
pub fn with_collider_color(mut self, color: Color) -> Self {
self.collider_color = Some(color);
self
}
pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
self.sleeping_color_multiplier = Some(color_multiplier);
self
}
pub fn with_contact_point_color(mut self, color: Color) -> Self {
self.contact_point_color = Some(color);
self
}
pub fn with_contact_normal_color(mut self, color: Color) -> Self {
self.contact_normal_color = Some(color);
self
}
pub fn with_contact_normal_scale(mut self, scale: ContactGizmoScale) -> Self {
self.contact_normal_scale = scale;
self
}
pub fn with_joint_colors(anchor_color: Option<Color>, separation_color: Option<Color>) -> Self {
Self {
joint_anchor_color: anchor_color,
joint_separation_color: separation_color,
..Self::none()
}
}
pub fn with_raycast_colors(
mut self,
ray: Option<Color>,
hit_point: Option<Color>,
hit_normal: Option<Color>,
) -> Self {
self.raycast_color = ray;
self.raycast_point_color = hit_point;
self.raycast_normal_color = hit_normal;
self
}
pub fn with_shapecast_colors(
mut self,
ray: Option<Color>,
shape: Option<Color>,
hit_point: Option<Color>,
hit_normal: Option<Color>,
) -> Self {
self.shapecast_color = ray;
self.shapecast_shape_color = shape;
self.shapecast_point_color = hit_point;
self.shapecast_normal_color = hit_normal;
self
}
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_meshes = !is_visible;
self
}
pub fn without_axes(mut self) -> Self {
self.axis_lengths = None;
self
}
pub fn without_aabbs(mut self) -> Self {
self.aabb_color = None;
self
}
pub fn without_colliders(mut self) -> Self {
self.collider_color = None;
self
}
pub fn without_contact_points(mut self) -> Self {
self.contact_point_color = None;
self
}
pub fn without_contact_normals(mut self) -> Self {
self.contact_normal_color = None;
self
}
pub fn without_joints(mut self) -> Self {
self.joint_anchor_color = None;
self.joint_separation_color = None;
self
}
pub fn without_raycasts(mut self) -> Self {
self.raycast_color = None;
self.raycast_point_color = None;
self.raycast_normal_color = None;
self
}
pub fn without_shapecasts(mut self) -> Self {
self.shapecast_color = None;
self.shapecast_shape_color = None;
self.shapecast_point_color = None;
self.shapecast_normal_color = None;
self
}
}
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
#[cfg_attr(feature = "2d", doc = " Collider::circle(0.5),")]
#[cfg_attr(feature = "3d", doc = " Collider::sphere(0.5),")]
#[derive(Component, Reflect, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Component, PartialEq)]
pub struct DebugRender {
pub axis_lengths: Option<Vector>,
pub aabb_color: Option<Color>,
pub collider_color: Option<Color>,
pub sleeping_color_multiplier: Option<[f32; 4]>,
pub hide_mesh: bool,
}
impl Default for DebugRender {
fn default() -> Self {
Self {
#[cfg(feature = "2d")]
axis_lengths: Some(Vector::new(5.0, 5.0)),
#[cfg(feature = "3d")]
axis_lengths: Some(Vector::new(0.5, 0.5, 0.5)),
aabb_color: None,
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
hide_mesh: false,
}
}
}
impl DebugRender {
pub fn all() -> Self {
Self {
#[cfg(feature = "2d")]
axis_lengths: Some(Vector::new(5.0, 5.0)),
#[cfg(feature = "3d")]
axis_lengths: Some(Vector::new(0.5, 0.5, 0.5)),
aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
hide_mesh: true,
}
}
pub fn none() -> Self {
Self {
axis_lengths: None,
aabb_color: None,
collider_color: None,
sleeping_color_multiplier: None,
hide_mesh: false,
}
}
pub fn axes(axis_lengths: Vector) -> Self {
Self {
axis_lengths: Some(axis_lengths),
..Self::none()
}
}
pub fn aabb(color: Color) -> Self {
Self {
aabb_color: Some(color),
..Self::none()
}
}
pub fn collider(color: Color) -> Self {
Self {
collider_color: Some(color),
..Self::none()
}
}
pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
self.axis_lengths = Some(axis_lengths);
self
}
pub fn with_aabb_color(mut self, color: Color) -> Self {
self.aabb_color = Some(color);
self
}
pub fn with_collider_color(mut self, color: Color) -> Self {
self.collider_color = Some(color);
self
}
pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
self.sleeping_color_multiplier = Some(color_multiplier);
self
}
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_mesh = !is_visible;
self
}
pub fn without_axes(mut self) -> Self {
self.axis_lengths = None;
self
}
pub fn without_aabb(mut self) -> Self {
self.aabb_color = None;
self
}
pub fn without_collider(mut self) -> Self {
self.collider_color = None;
self
}
}