1use crate::prelude::*;
2use bevy::{color::palettes::css::*, prelude::*};
3
4#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
15#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
16#[cfg_attr(
40 feature = "2d",
41 doc = " commands.spawn((RigidBody::Dynamic, Collider::circle(0.5)));"
42)]
43#[cfg_attr(
44 feature = "3d",
45 doc = " commands.spawn((RigidBody::Dynamic, Collider::sphere(0.5)));"
46)]
47#[derive(Reflect, GizmoConfigGroup)]
50#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
51#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
52pub struct PhysicsGizmos {
53 pub axis_lengths: Option<Vector>,
55 pub aabb_color: Option<Color>,
57 pub collider_color: Option<Color>,
59 pub sleeping_color_multiplier: Option<[f32; 4]>,
62 pub contact_point_color: Option<Color>,
64 pub contact_normal_color: Option<Color>,
66 pub contact_normal_scale: ContactGizmoScale,
68 pub joint_anchor_color: Option<Color>,
70 pub joint_separation_color: Option<Color>,
72 pub raycast_color: Option<Color>,
74 pub raycast_point_color: Option<Color>,
76 pub raycast_normal_color: Option<Color>,
78 pub shapecast_color: Option<Color>,
80 pub shapecast_shape_color: Option<Color>,
82 pub shapecast_point_color: Option<Color>,
84 pub shapecast_normal_color: Option<Color>,
86 pub island_color: Option<Color>,
88 pub hide_meshes: bool,
91}
92
93impl Default for PhysicsGizmos {
94 fn default() -> Self {
95 Self {
96 axis_lengths: Some(Vector::splat(0.5)),
97 aabb_color: None,
98 collider_color: Some(ORANGE.into()),
99 sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
100 contact_point_color: None,
101 contact_normal_color: None,
102 contact_normal_scale: ContactGizmoScale::default(),
103 joint_anchor_color: Some(PINK.into()),
104 joint_separation_color: Some(RED.into()),
105 raycast_color: Some(RED.into()),
106 raycast_point_color: Some(YELLOW.into()),
107 raycast_normal_color: Some(PINK.into()),
108 shapecast_color: Some(RED.into()),
109 shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
110 shapecast_point_color: Some(YELLOW.into()),
111 shapecast_normal_color: Some(PINK.into()),
112 island_color: None,
113 hide_meshes: false,
114 }
115 }
116}
117
118#[derive(Reflect, Clone, Copy, PartialEq)]
120#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
121#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
122#[reflect(PartialEq)]
123pub enum ContactGizmoScale {
124 Constant(Scalar),
126 Scaled(Scalar),
128}
129
130impl Default for ContactGizmoScale {
131 fn default() -> Self {
132 Self::Scaled(0.025)
133 }
134}
135
136impl PhysicsGizmos {
137 pub fn all() -> Self {
139 Self {
140 axis_lengths: Some(Vector::splat(0.5)),
141 aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
142 collider_color: Some(ORANGE.into()),
143 sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
144 contact_point_color: Some(LIGHT_CYAN.into()),
145 contact_normal_color: Some(RED.into()),
146 contact_normal_scale: ContactGizmoScale::default(),
147 joint_anchor_color: Some(PINK.into()),
148 joint_separation_color: Some(RED.into()),
149 raycast_color: Some(RED.into()),
150 raycast_point_color: Some(YELLOW.into()),
151 raycast_normal_color: Some(PINK.into()),
152 shapecast_color: Some(RED.into()),
153 shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
154 shapecast_point_color: Some(YELLOW.into()),
155 shapecast_normal_color: Some(PINK.into()),
156 island_color: Some(RED.into()),
157 hide_meshes: true,
158 }
159 }
160
161 pub fn none() -> Self {
165 Self {
166 axis_lengths: None,
167 aabb_color: None,
168 collider_color: None,
169 sleeping_color_multiplier: None,
170 contact_point_color: None,
171 contact_normal_color: None,
172 contact_normal_scale: ContactGizmoScale::default(),
173 joint_anchor_color: None,
174 joint_separation_color: None,
175 raycast_color: None,
176 raycast_point_color: None,
177 raycast_normal_color: None,
178 shapecast_color: None,
179 shapecast_shape_color: None,
180 shapecast_point_color: None,
181 shapecast_normal_color: None,
182 island_color: None,
183 hide_meshes: false,
184 }
185 }
186
187 pub fn axes(axis_lengths: Vector) -> Self {
190 Self {
191 axis_lengths: Some(axis_lengths),
192 ..Self::none()
193 }
194 }
195
196 pub fn aabbs(color: Color) -> Self {
199 Self {
200 aabb_color: Some(color),
201 ..Self::none()
202 }
203 }
204
205 pub fn colliders(color: Color) -> Self {
208 Self {
209 collider_color: Some(color),
210 ..Self::none()
211 }
212 }
213
214 pub fn contact_points(color: Color) -> Self {
217 Self {
218 contact_point_color: Some(color),
219 ..Self::none()
220 }
221 }
222
223 pub fn contact_normals(color: Color) -> Self {
226 Self {
227 contact_normal_color: Some(color),
228 ..Self::none()
229 }
230 }
231
232 pub fn joints(anchor_color: Option<Color>, separation_color: Option<Color>) -> Self {
235 Self {
236 joint_anchor_color: anchor_color,
237 joint_separation_color: separation_color,
238 ..Self::none()
239 }
240 }
241
242 pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
244 self.axis_lengths = Some(axis_lengths);
245 self
246 }
247
248 pub fn with_aabb_color(mut self, color: Color) -> Self {
250 self.aabb_color = Some(color);
251 self
252 }
253
254 pub fn with_collider_color(mut self, color: Color) -> Self {
256 self.collider_color = Some(color);
257 self
258 }
259
260 pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
262 self.sleeping_color_multiplier = Some(color_multiplier);
263 self
264 }
265
266 pub fn with_contact_point_color(mut self, color: Color) -> Self {
268 self.contact_point_color = Some(color);
269 self
270 }
271
272 pub fn with_contact_normal_color(mut self, color: Color) -> Self {
274 self.contact_normal_color = Some(color);
275 self
276 }
277
278 pub fn with_contact_normal_scale(mut self, scale: ContactGizmoScale) -> Self {
280 self.contact_normal_scale = scale;
281 self
282 }
283
284 pub fn with_joint_colors(anchor_color: Option<Color>, separation_color: Option<Color>) -> Self {
286 Self {
287 joint_anchor_color: anchor_color,
288 joint_separation_color: separation_color,
289 ..Self::none()
290 }
291 }
292
293 pub fn with_raycast_colors(
295 mut self,
296 ray: Option<Color>,
297 hit_point: Option<Color>,
298 hit_normal: Option<Color>,
299 ) -> Self {
300 self.raycast_color = ray;
301 self.raycast_point_color = hit_point;
302 self.raycast_normal_color = hit_normal;
303 self
304 }
305
306 pub fn with_shapecast_colors(
308 mut self,
309 ray: Option<Color>,
310 shape: Option<Color>,
311 hit_point: Option<Color>,
312 hit_normal: Option<Color>,
313 ) -> Self {
314 self.shapecast_color = ray;
315 self.shapecast_shape_color = shape;
316 self.shapecast_point_color = hit_point;
317 self.shapecast_normal_color = hit_normal;
318 self
319 }
320
321 pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
323 self.hide_meshes = !is_visible;
324 self
325 }
326
327 pub fn without_axes(mut self) -> Self {
329 self.axis_lengths = None;
330 self
331 }
332
333 pub fn without_aabbs(mut self) -> Self {
335 self.aabb_color = None;
336 self
337 }
338
339 pub fn without_colliders(mut self) -> Self {
341 self.collider_color = None;
342 self
343 }
344
345 pub fn without_contact_points(mut self) -> Self {
347 self.contact_point_color = None;
348 self
349 }
350
351 pub fn without_contact_normals(mut self) -> Self {
353 self.contact_normal_color = None;
354 self
355 }
356
357 pub fn without_joints(mut self) -> Self {
359 self.joint_anchor_color = None;
360 self.joint_separation_color = None;
361 self
362 }
363
364 pub fn without_raycasts(mut self) -> Self {
366 self.raycast_color = None;
367 self.raycast_point_color = None;
368 self.raycast_normal_color = None;
369 self
370 }
371
372 pub fn without_shapecasts(mut self) -> Self {
374 self.shapecast_color = None;
375 self.shapecast_shape_color = None;
376 self.shapecast_point_color = None;
377 self.shapecast_normal_color = None;
378 self
379 }
380}
381
382#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
390#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
391#[cfg_attr(feature = "2d", doc = " Collider::circle(0.5),")]
409#[cfg_attr(feature = "3d", doc = " Collider::sphere(0.5),")]
410#[derive(Component, Reflect, Clone, Copy, PartialEq)]
416#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
417#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
418#[reflect(Component, PartialEq)]
419pub struct DebugRender {
420 pub axis_lengths: Option<Vector>,
422 pub aabb_color: Option<Color>,
424 pub collider_color: Option<Color>,
426 pub sleeping_color_multiplier: Option<[f32; 4]>,
429 pub hide_mesh: bool,
431}
432
433impl Default for DebugRender {
434 fn default() -> Self {
435 Self {
436 #[cfg(feature = "2d")]
437 axis_lengths: Some(Vector::new(5.0, 5.0)),
438 #[cfg(feature = "3d")]
439 axis_lengths: Some(Vector::new(0.5, 0.5, 0.5)),
440 aabb_color: None,
441 collider_color: Some(ORANGE.into()),
442 sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
443 hide_mesh: false,
444 }
445 }
446}
447
448impl DebugRender {
449 pub fn all() -> Self {
451 Self {
452 #[cfg(feature = "2d")]
453 axis_lengths: Some(Vector::new(5.0, 5.0)),
454 #[cfg(feature = "3d")]
455 axis_lengths: Some(Vector::new(0.5, 0.5, 0.5)),
456 aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
457 collider_color: Some(ORANGE.into()),
458 sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
459 hide_mesh: true,
460 }
461 }
462
463 pub fn none() -> Self {
465 Self {
466 axis_lengths: None,
467 aabb_color: None,
468 collider_color: None,
469 sleeping_color_multiplier: None,
470 hide_mesh: false,
471 }
472 }
473
474 pub fn axes(axis_lengths: Vector) -> Self {
477 Self {
478 axis_lengths: Some(axis_lengths),
479 ..Self::none()
480 }
481 }
482
483 pub fn aabb(color: Color) -> Self {
486 Self {
487 aabb_color: Some(color),
488 ..Self::none()
489 }
490 }
491
492 pub fn collider(color: Color) -> Self {
495 Self {
496 collider_color: Some(color),
497 ..Self::none()
498 }
499 }
500
501 pub fn with_axes(mut self, axis_lengths: Vector) -> Self {
503 self.axis_lengths = Some(axis_lengths);
504 self
505 }
506
507 pub fn with_aabb_color(mut self, color: Color) -> Self {
509 self.aabb_color = Some(color);
510 self
511 }
512
513 pub fn with_collider_color(mut self, color: Color) -> Self {
515 self.collider_color = Some(color);
516 self
517 }
518
519 pub fn with_sleeping_color_multiplier(mut self, color_multiplier: [f32; 4]) -> Self {
521 self.sleeping_color_multiplier = Some(color_multiplier);
522 self
523 }
524
525 pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
527 self.hide_mesh = !is_visible;
528 self
529 }
530
531 pub fn without_axes(mut self) -> Self {
533 self.axis_lengths = None;
534 self
535 }
536
537 pub fn without_aabb(mut self) -> Self {
539 self.aabb_color = None;
540 self
541 }
542
543 pub fn without_collider(mut self) -> Self {
545 self.collider_color = None;
546 self
547 }
548}