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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
use crate::prelude::*;
use bevy::{color::palettes::css::*, prelude::*};
/// Gizmos used for debug rendering physics. See [`PhysicsDebugPlugin`]
///
/// You can configure what is rendered by inserting the [`PhysicsGizmos`] configuration group
/// or by getting it from the `GizmoConfigStore` resource and mutating the returned configuration.
///
/// To configure the debug rendering of specific entities, use the [`DebugRender`] component.
///
/// # Example
///
/// ```no_run
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// fn main() {
/// App::new()
/// .add_plugins((
/// DefaultPlugins,
/// PhysicsPlugins::default(),
/// // Enables debug rendering
/// PhysicsDebugPlugin::default(),
/// ))
/// // Overwrite default debug rendering configuration (optional)
/// .insert_gizmo_config(
/// PhysicsGizmos {
/// aabb_color: Some(Color::WHITE),
/// ..default()
/// },
/// GizmoConfig::default(),
/// )
/// .run();
/// }
///
/// fn setup(mut commands: Commands) {
/// // This rigid body and its collider and AABB will get rendered
#[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 {
/// The lengths of the axes drawn for an entity at the center of mass.
pub axis_lengths: Option<Vector>,
/// The color of the [AABBs](ColliderAabb). If `None`, the AABBs will not be rendered.
pub aabb_color: Option<Color>,
/// The color of the [collider](Collider) wireframes. If `None`, the colliders will not be rendered.
pub collider_color: Option<Color>,
/// The colors (in HSLA) for [sleeping](Sleeping) bodies will be multiplied by this array.
/// If `None`, sleeping will have no effect on the colors.
pub sleeping_color_multiplier: Option<[f32; 4]>,
/// The color of the contact points. If `None`, the contact points will not be rendered.
pub contact_point_color: Option<Color>,
/// The color of the contact normals. If `None`, the contact normals will not be rendered.
pub contact_normal_color: Option<Color>,
/// The scale used for contact normals.
pub contact_normal_scale: ContactGizmoScale,
/// The color of the lines drawn from the centers of bodies to their joint anchors.
pub joint_anchor_color: Option<Color>,
/// The color of the lines drawn between joint anchors, indicating the separation.
pub joint_separation_color: Option<Color>,
/// The color used for the rays in [raycasts](spatial_query#raycasting).
pub raycast_color: Option<Color>,
/// The color used for the hit points in [raycasts](spatial_query#raycasting).
pub raycast_point_color: Option<Color>,
/// The color used for the hit normals in [raycasts](spatial_query#raycasting).
pub raycast_normal_color: Option<Color>,
/// The color used for the ray in [shapecasts](spatial_query#shapecasting).
pub shapecast_color: Option<Color>,
/// The color used for the shape in [shapecasts](spatial_query#shapecasting).
pub shapecast_shape_color: Option<Color>,
/// The color used for the hit points in [shapecasts](spatial_query#shapecasting).
pub shapecast_point_color: Option<Color>,
/// The color used for the hit normals in [shapecasts](spatial_query#shapecasting).
pub shapecast_normal_color: Option<Color>,
/// Determines if the visibility of entities with [colliders](Collider) should be set to `Visibility::Hidden`,
/// which will only show the debug renders.
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()),
hide_meshes: false,
}
}
}
/// The scale used for contact normals rendered using gizmos.
#[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 {
/// The length of the rendered contact normal is constant.
Constant(Scalar),
/// The length of the rendered contact normal is scaled by the contact force and the given scaling factor.
Scaled(Scalar),
}
impl Default for ContactGizmoScale {
fn default() -> Self {
Self::Scaled(0.025)
}
}
impl PhysicsGizmos {
/// Creates a [`PhysicsGizmos`] configuration with all rendering options enabled.
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()),
hide_meshes: true,
}
}
/// Creates a [`PhysicsGizmos`] configuration with debug rendering enabled but all options turned off.
///
/// Note: this doesn't affect entities with [`DebugRender`] component; their debug gizmos will be visible.
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,
hide_meshes: false,
}
}
/// Creates a [`PhysicsGizmos`] configuration with the given lengths for the axes
/// that are drawn for the entity at the center of mass. Other debug rendering options will be disabled.
pub fn axes(axis_lengths: Vector) -> Self {
Self {
axis_lengths: Some(axis_lengths),
..Self::none()
}
}
/// Creates a [`PhysicsGizmos`] configuration with a given AABB color.
/// Other debug rendering options will be disabled.
pub fn aabbs(color: Color) -> Self {
Self {
aabb_color: Some(color),
..Self::none()
}
}
/// Creates a [`PhysicsGizmos`] configuration with a given collider color.
/// Other debug rendering options will be disabled.
pub fn colliders(color: Color) -> Self {
Self {
collider_color: Some(color),
..Self::none()
}
}
/// Creates a [`PhysicsGizmos`] configuration with a given contact point color.
/// Other debug rendering options will be disabled.
pub fn contact_points(color: Color) -> Self {
Self {
contact_point_color: Some(color),
..Self::none()
}
}
/// Creates a [`PhysicsGizmos`] configuration with a given contact normal color.
/// Other debug rendering options will be disabled.
pub fn contact_normals(color: Color) -> Self {
Self {
contact_normal_color: Some(color),
..Self::none()
}
}
/// Creates a [`PhysicsGizmos`] configuration with given colors for
/// joint anchors and separation distances. Other debug rendering options will be disabled.
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()
}
}
/// Sets the lengths of the axes drawn for the entity.
pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
self.axis_lengths = Some(axis_lengths);
self
}
/// Sets the AABB color.
pub fn with_aabb_color(mut self, color: Color) -> Self {
self.aabb_color = Some(color);
self
}
/// Sets the collider color.
pub fn with_collider_color(mut self, color: Color) -> Self {
self.collider_color = Some(color);
self
}
/// Sets the multiplier used for the colors (in HSLA) of [sleeping](Sleeping) bodies.
pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
self.sleeping_color_multiplier = Some(color_multiplier);
self
}
/// Sets the contact point color.
pub fn with_contact_point_color(mut self, color: Color) -> Self {
self.contact_point_color = Some(color);
self
}
/// Sets the contact normal color.
pub fn with_contact_normal_color(mut self, color: Color) -> Self {
self.contact_normal_color = Some(color);
self
}
/// Sets the contact normal scale.
pub fn with_contact_normal_scale(mut self, scale: ContactGizmoScale) -> Self {
self.contact_normal_scale = scale;
self
}
/// Sets the colors used for debug rendering joints.
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()
}
}
/// Sets the colors used for debug rendering raycasts.
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
}
/// Sets the colors used for debug rendering shapecasts.
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
}
/// Sets the visibility of the entity's visual mesh.
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_meshes = !is_visible;
self
}
/// Disables axis debug rendering.
pub fn without_axes(mut self) -> Self {
self.axis_lengths = None;
self
}
/// Disables AABB debug rendering.
pub fn without_aabbs(mut self) -> Self {
self.aabb_color = None;
self
}
/// Disables collider debug rendering.
pub fn without_colliders(mut self) -> Self {
self.collider_color = None;
self
}
/// Disables contact point debug rendering.
pub fn without_contact_points(mut self) -> Self {
self.contact_point_color = None;
self
}
/// Disables contact normal debug rendering.
pub fn without_contact_normals(mut self) -> Self {
self.contact_normal_color = None;
self
}
/// Disables joint debug rendering.
pub fn without_joints(mut self) -> Self {
self.joint_anchor_color = None;
self.joint_separation_color = None;
self
}
/// Disables raycast debug rendering.
pub fn without_raycasts(mut self) -> Self {
self.raycast_color = None;
self.raycast_point_color = None;
self.raycast_normal_color = None;
self
}
/// Disables shapecast debug rendering.
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
}
}
/// A component for the debug render configuration of an entity. See [`PhysicsDebugPlugin`].
///
/// This overwrites the global [`PhysicsGizmos`] for this specific entity.
///
/// ## Example
///
/// ```no_run
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// fn main() {
/// App::new()
/// .add_plugins((
/// DefaultPlugins,
/// PhysicsPlugins::default(),
/// // Enables debug rendering
/// PhysicsDebugPlugin::default(),
/// ))
/// .run();
/// }
///
/// fn setup(mut commands: Commands) {
/// // This rigid body and its collider and AABB will get rendered
/// commands.spawn((
/// RigidBody::Dynamic,
#[cfg_attr(feature = "2d", doc = " Collider::circle(0.5),")]
#[cfg_attr(feature = "3d", doc = " Collider::sphere(0.5),")]
/// // Overwrite default collider color (optional)
/// DebugRender::default().with_collider_color(Color::srgb(1.0, 0.0, 0.0)),
/// ));
/// }
/// ```
#[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 {
/// The lengths of the axes drawn for the entity at the center of mass.
pub axis_lengths: Option<Vector>,
/// The color of the [AABB](ColliderAabb). If `None`, the AABB will not be rendered.
pub aabb_color: Option<Color>,
/// The color of the [collider](Collider) wireframe. If `None`, the collider will not be rendered.
pub collider_color: Option<Color>,
/// If the entity is [sleeping](Sleeping), its colors (in HSLA) will be multiplied by this array.
/// If `None`, sleeping will have no effect on the colors.
pub sleeping_color_multiplier: Option<[f32; 4]>,
/// Determines if the entity's visibility should be set to `Visibility::Hidden`, which will only show the debug render.
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 {
/// Creates a [`DebugRender`] configuration with all rendering options enabled.
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,
}
}
/// Disables all debug rendering for this entity.
pub fn none() -> Self {
Self {
axis_lengths: None,
aabb_color: None,
collider_color: None,
sleeping_color_multiplier: None,
hide_mesh: false,
}
}
/// Creates a [`DebugRender`] configuration with the given lengths for the axes
/// that are drawn for the entity at the center of mass. Other debug rendering options will be disabled.
pub fn axes(axis_lengths: Vector) -> Self {
Self {
axis_lengths: Some(axis_lengths),
..Self::none()
}
}
/// Creates a [`DebugRender`] configuration with a given AABB color.
/// Other debug rendering options will be disabled.
pub fn aabb(color: Color) -> Self {
Self {
aabb_color: Some(color),
..Self::none()
}
}
/// Creates a [`DebugRender`] configuration with a given collider color.
/// Other debug rendering options will be disabled.
pub fn collider(color: Color) -> Self {
Self {
collider_color: Some(color),
..Self::none()
}
}
/// Sets the lengths of the axes drawn for the entity at the center of mass.
pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
self.axis_lengths = Some(axis_lengths);
self
}
/// Sets the AABB color.
pub fn with_aabb_color(mut self, color: Color) -> Self {
self.aabb_color = Some(color);
self
}
/// Sets the collider color.
pub fn with_collider_color(mut self, color: Color) -> Self {
self.collider_color = Some(color);
self
}
/// Sets the multiplier used for the colors (in HSLA) of [sleeping](Sleeping) bodies.
pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
self.sleeping_color_multiplier = Some(color_multiplier);
self
}
/// Sets the visibility of the entity's visual mesh.
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_mesh = !is_visible;
self
}
/// Disables axis debug rendering.
pub fn without_axes(mut self) -> Self {
self.axis_lengths = None;
self
}
/// Disables AABB debug rendering.
pub fn without_aabb(mut self) -> Self {
self.aabb_color = None;
self
}
/// Disables collider debug rendering.
pub fn without_collider(mut self) -> Self {
self.collider_color = None;
self
}
}