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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
use crate::prelude::*;
use bevy::{ecs::system::SystemParam, prelude::*};
/// A system parameter for performing [spatial queries](spatial_query).
///
/// ## Methods
///
/// - [Raycasting](spatial_query#raycasting): [`cast_ray`](SpatialQuery::cast_ray),
/// [`ray_hits`](SpatialQuery::ray_hits), [`ray_hits_callback`](SpatialQuery::ray_hits_callback)
/// - [Shapecasting](spatial_query#shapecasting): [`cast_shape`](SpatialQuery::cast_shape),
/// [`shape_hits`](SpatialQuery::shape_hits), [`shape_hits_callback`](SpatialQuery::shape_hits_callback)
/// - [Point projection](spatial_query#point-projection): [`project_point`](SpatialQuery::project_point)
/// - [Intersection tests](spatial_query#intersection-tests)
/// - Point intersections: [`point_intersections`](SpatialQuery::point_intersections),
/// [`point_intersections_callback`](SpatialQuery::point_intersections_callback)
/// - AABB intersections: [`aabb_intersections_with_aabb`](SpatialQuery::aabb_intersections_with_aabb),
/// [`aabb_intersections_with_aabb_callback`](SpatialQuery::aabb_intersections_with_aabb_callback)
/// - Shape intersections: [`shape_intersections`](SpatialQuery::shape_intersections)
/// [`shape_intersections_callback`](SpatialQuery::shape_intersections_callback)
///
/// For simple raycasts and shapecasts, consider using the [`RayCaster`] and [`ShapeCaster`] components that
/// provide a more ECS-based approach and perform casts on every frame.
///
/// ## Raycasting example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// // Cast ray and print first hit
/// if let Some(first_hit) = spatial_query.cast_ray(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// ) {
/// println!("First hit: {:?}", first_hit);
/// }
///
/// // Cast ray and get up to 20 hits
/// let hits = spatial_query.ray_hits(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// 20, // Maximum number of hits
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// );
///
/// // Print hits
/// for hit in hits.iter() {
/// println!("Hit: {:?}", hit);
/// }
/// }
/// ```
#[derive(SystemParam)]
pub struct SpatialQuery<'w, 's> {
pub(crate) colliders: Query<
'w,
's,
(
Entity,
&'static Position,
&'static Rotation,
&'static Collider,
Option<&'static CollisionLayers>,
),
>,
pub(crate) added_colliders: Query<'w, 's, Entity, Added<Collider>>,
/// The [`SpatialQueryPipeline`].
pub query_pipeline: ResMut<'w, SpatialQueryPipeline>,
}
impl<'w, 's> SpatialQuery<'w, 's> {
/// Updates the colliders in the pipeline. This is done automatically once per physics frame in
/// [`PhysicsStepSet::SpatialQuery`], but if you modify colliders or their positions before that, you can
/// call this to make sure the data is up to date when performing spatial queries using [`SpatialQuery`].
pub fn update_pipeline(&mut self) {
self.query_pipeline
.update(self.colliders.iter(), self.added_colliders.iter());
}
/// Casts a [ray](spatial_query#raycasting) and computes the closest [hit](RayHitData) with a collider.
/// If there are no hits, `None` is returned.
///
/// ## Arguments
///
/// - `origin`: Where the ray is cast from.
/// - `direction`: What direction the ray is cast in.
/// - `max_time_of_impact`: The maximum distance that the ray can travel.
/// - `solid`: If true and the ray origin is inside of a collider, the hit point will be the ray origin itself.
/// Otherwise, the collider will be treated as hollow, and the hit point will be at the collider's boundary.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// // Cast ray and print first hit
/// if let Some(first_hit) = spatial_query.cast_ray(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// ) {
/// println!("First hit: {:?}", first_hit);
/// }
/// }
/// ```
pub fn cast_ray(
&self,
origin: Vector,
direction: Dir,
max_time_of_impact: Scalar,
solid: bool,
query_filter: SpatialQueryFilter,
) -> Option<RayHitData> {
self.query_pipeline
.cast_ray(origin, direction, max_time_of_impact, solid, query_filter)
}
/// Casts a [ray](spatial_query#raycasting) and computes the closest [hit](RayHitData) with a collider.
/// If there are no hits, `None` is returned.
///
/// ## Arguments
///
/// - `origin`: Where the ray is cast from.
/// - `direction`: What direction the ray is cast in.
/// - `max_time_of_impact`: The maximum distance that the ray can travel.
/// - `solid`: If true and the ray origin is inside of a collider, the hit point will be the ray origin itself.
/// Otherwise, the collider will be treated as hollow, and the hit point will be at the collider's boundary.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `predicate`: A function with which the colliders are filtered. Given the Entity it should return false, if the
/// entity should be ignored.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// #[derive(Component)]
/// struct Invisible;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery, query: Query<&Invisible>) {
/// // Cast ray and print first hit
/// if let Some(first_hit) = spatial_query.cast_ray_predicate(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// &|entity| { // Predicate
/// // Skip entities with the `Invisible` component.
/// !query.contains(entity)
/// }
/// ) {
/// println!("First hit: {:?}", first_hit);
/// }
/// }
/// ```
pub fn cast_ray_predicate(
&self,
origin: Vector,
direction: Dir,
max_time_of_impact: Scalar,
solid: bool,
query_filter: SpatialQueryFilter,
predicate: &dyn Fn(Entity) -> bool,
) -> Option<RayHitData> {
self.query_pipeline.cast_ray_predicate(
origin,
direction,
max_time_of_impact,
solid,
query_filter,
predicate,
)
}
/// Casts a [ray](spatial_query#raycasting) and computes all [hits](RayHitData) until `max_hits` is reached.
///
/// Note that the order of the results is not guaranteed, and if there are more hits than `max_hits`,
/// some hits will be missed.
///
/// ## Arguments
///
/// - `origin`: Where the ray is cast from.
/// - `direction`: What direction the ray is cast in.
/// - `max_time_of_impact`: The maximum distance that the ray can travel.
/// - `max_hits`: The maximum number of hits. Additional hits will be missed.
/// - `solid`: If true and the ray origin is inside of a collider, the hit point will be the ray origin itself.
/// Otherwise, the collider will be treated as hollow, and the hit point will be at the collider's boundary.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// // Cast ray and get hits
/// let hits = spatial_query.ray_hits(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// 20, // Maximum number of hits
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// );
///
/// // Print hits
/// for hit in hits.iter() {
/// println!("Hit: {:?}", hit);
/// }
/// }
/// ```
pub fn ray_hits(
&self,
origin: Vector,
direction: Dir,
max_time_of_impact: Scalar,
max_hits: u32,
solid: bool,
query_filter: SpatialQueryFilter,
) -> Vec<RayHitData> {
self.query_pipeline.ray_hits(
origin,
direction,
max_time_of_impact,
max_hits,
solid,
query_filter,
)
}
/// Casts a [ray](spatial_query#raycasting) and computes all [hits](RayHitData), calling the given `callback`
/// for each hit. The raycast stops when `callback` returns false or all hits have been found.
///
/// Note that the order of the results is not guaranteed.
///
/// ## Arguments
///
/// - `origin`: Where the ray is cast from.
/// - `direction`: What direction the ray is cast in.
/// - `max_time_of_impact`: The maximum distance that the ray can travel.
/// - `solid`: If true and the ray origin is inside of a collider, the hit point will be the ray origin itself.
/// Otherwise, the collider will be treated as hollow, and the hit point will be at the collider's boundary.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `callback`: A callback function called for each hit.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// let mut hits = vec![];
///
/// // Cast ray and get all hits
/// spatial_query.ray_hits_callback(
/// Vec3::ZERO, // Origin
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Does the ray treat colliders as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// |hit| { // Callback function
/// hits.push(hit);
/// true
/// },
/// );
///
/// // Print hits
/// for hit in hits.iter() {
/// println!("Hit: {:?}", hit);
/// }
/// }
/// ```
pub fn ray_hits_callback(
&self,
origin: Vector,
direction: Dir,
max_time_of_impact: Scalar,
solid: bool,
query_filter: SpatialQueryFilter,
callback: impl FnMut(RayHitData) -> bool,
) {
self.query_pipeline.ray_hits_callback(
origin,
direction,
max_time_of_impact,
solid,
query_filter,
callback,
)
}
/// Casts a [shape](spatial_query#shapecasting) with a given rotation and computes the closest [hit](ShapeHits)
/// with a collider. If there are no hits, `None` is returned.
///
/// For a more ECS-based approach, consider using the [`ShapeCaster`] component instead.
///
/// ## Arguments
///
/// - `shape`: The shape being cast represented as a [`Collider`].
/// - `origin`: Where the shape is cast from.
/// - `shape_rotation`: The rotation of the shape being cast.
/// - `direction`: What direction the shape is cast in.
/// - `max_time_of_impact`: The maximum distance that the shape can travel.
/// - `ignore_origin_penetration`: If true and the shape is already penetrating a collider at the
/// shape origin, the hit will be ignored and only the next hit will be computed. Otherwise, the initial
/// hit will be returned.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// // Cast ray and print first hit
/// if let Some(first_hit) = spatial_query.cast_shape(
/// &Collider::sphere(0.5), // Shape
/// Vec3::ZERO, // Origin
/// Quat::default(), // Shape rotation
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Should initial penetration at the origin be ignored
/// SpatialQueryFilter::default(), // Query filter
/// ) {
/// println!("First hit: {:?}", first_hit);
/// }
/// }
/// ```
#[allow(clippy::too_many_arguments)]
pub fn cast_shape(
&self,
shape: &Collider,
origin: Vector,
shape_rotation: RotationValue,
direction: Dir,
max_time_of_impact: Scalar,
ignore_origin_penetration: bool,
query_filter: SpatialQueryFilter,
) -> Option<ShapeHitData> {
self.query_pipeline.cast_shape(
shape,
origin,
shape_rotation,
direction,
max_time_of_impact,
ignore_origin_penetration,
query_filter,
)
}
/// Casts a [shape](spatial_query#shapecasting) with a given rotation and computes computes all [hits](ShapeHitData)
/// in the order of the time of impact until `max_hits` is reached.
///
/// ## Arguments
///
/// - `shape`: The shape being cast represented as a [`Collider`].
/// - `origin`: Where the shape is cast from.
/// - `shape_rotation`: The rotation of the shape being cast.
/// - `direction`: What direction the shape is cast in.
/// - `max_time_of_impact`: The maximum distance that the shape can travel.
/// - `max_hits`: The maximum number of hits. Additional hits will be missed.
/// - `ignore_origin_penetration`: If true and the shape is already penetrating a collider at the
/// shape origin, the hit will be ignored and only the next hit will be computed. Otherwise, the initial
/// hit will be returned.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `callback`: A callback function called for each hit.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// // Cast shape and get all hits
/// let hits = spatial_query.shape_hits(
/// &Collider::sphere(0.5), // Shape
/// Vec3::ZERO, // Origin
/// Quat::default(), // Shape rotation
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// 20, // Max hits
/// true, // Should initial penetration at the origin be ignored
/// SpatialQueryFilter::default(), // Query filter
/// );
///
/// // Print hits
/// for hit in hits.iter() {
/// println!("Hit: {:?}", hit);
/// }
/// }
/// ```
#[allow(clippy::too_many_arguments)]
pub fn shape_hits(
&self,
shape: &Collider,
origin: Vector,
shape_rotation: RotationValue,
direction: Dir,
max_time_of_impact: Scalar,
max_hits: u32,
ignore_origin_penetration: bool,
query_filter: SpatialQueryFilter,
) -> Vec<ShapeHitData> {
self.query_pipeline.shape_hits(
shape,
origin,
shape_rotation,
direction,
max_time_of_impact,
max_hits,
ignore_origin_penetration,
query_filter,
)
}
/// Casts a [shape](spatial_query#shapecasting) with a given rotation and computes computes all [hits](ShapeHitData)
/// in the order of the time of impact, calling the given `callback` for each hit. The shapecast stops when
/// `callback` returns false or all hits have been found.
///
/// ## Arguments
///
/// - `shape`: The shape being cast represented as a [`Collider`].
/// - `origin`: Where the shape is cast from.
/// - `shape_rotation`: The rotation of the shape being cast.
/// - `direction`: What direction the shape is cast in.
/// - `max_time_of_impact`: The maximum distance that the shape can travel.
/// - `ignore_origin_penetration`: If true and the shape is already penetrating a collider at the
/// shape origin, the hit will be ignored and only the next hit will be computed. Otherwise, the initial
/// hit will be returned.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `callback`: A callback function called for each hit.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_hits(spatial_query: SpatialQuery) {
/// let mut hits = vec![];
///
/// // Cast shape and get all hits
/// spatial_query.shape_hits_callback(
/// &Collider::sphere(0.5), // Shape
/// Vec3::ZERO, // Origin
/// Quat::default(), // Shape rotation
/// Dir3::X, // Direction
/// 100.0, // Maximum time of impact (travel distance)
/// true, // Should initial penetration at the origin be ignored
/// SpatialQueryFilter::default(), // Query filter
/// |hit| { // Callback function
/// hits.push(hit);
/// true
/// },
/// );
///
/// // Print hits
/// for hit in hits.iter() {
/// println!("Hit: {:?}", hit);
/// }
/// }
/// ```
#[allow(clippy::too_many_arguments)]
pub fn shape_hits_callback(
&self,
shape: &Collider,
origin: Vector,
shape_rotation: RotationValue,
direction: Dir,
max_time_of_impact: Scalar,
ignore_origin_penetration: bool,
query_filter: SpatialQueryFilter,
callback: impl FnMut(ShapeHitData) -> bool,
) {
self.query_pipeline.shape_hits_callback(
shape,
origin,
shape_rotation,
direction,
max_time_of_impact,
ignore_origin_penetration,
query_filter,
callback,
)
}
/// Finds the [projection](spatial_query#point-projection) of a given point on the closest [collider](Collider).
/// If one isn't found, `None` is returned.
///
/// ## Arguments
///
/// - `point`: The point that should be projected.
/// - `solid`: If true and the point is inside of a collider, the projection will be at the point.
/// Otherwise, the collider will be treated as hollow, and the projection will be at the collider's boundary.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_point_projection(spatial_query: SpatialQuery) {
/// // Project a point and print the result
/// if let Some(projection) = spatial_query.project_point(
/// Vec3::ZERO, // Point
/// true, // Are colliders treated as "solid"
/// SpatialQueryFilter::default(), // Query filter
/// ) {
/// println!("Projection: {:?}", projection);
/// }
/// }
/// ```
pub fn project_point(
&self,
point: Vector,
solid: bool,
query_filter: SpatialQueryFilter,
) -> Option<PointProjection> {
self.query_pipeline
.project_point(point, solid, query_filter)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [collider](Collider)
/// that contains the given point.
///
/// ## Arguments
///
/// - `point`: The point that intersections are tested against.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_point_intersections(spatial_query: SpatialQuery) {
/// let intersections =
/// spatial_query.point_intersections(Vec3::ZERO, SpatialQueryFilter::default());
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn point_intersections(
&self,
point: Vector,
query_filter: SpatialQueryFilter,
) -> Vec<Entity> {
self.query_pipeline.point_intersections(point, query_filter)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [collider](Collider)
/// that contains the given point, calling the given `callback` for each intersection.
/// The search stops when `callback` returns `false` or all intersections have been found.
///
/// ## Arguments
///
/// - `point`: The point that intersections are tested against.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `callback`: A callback function called for each intersection.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_point_intersections(spatial_query: SpatialQuery) {
/// let mut intersections = vec![];
///
/// spatial_query.point_intersections_callback(
/// Vec3::ZERO, // Point
/// SpatialQueryFilter::default(), // Query filter
/// |entity| { // Callback function
/// intersections.push(entity);
/// true
/// },
/// );
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn point_intersections_callback(
&self,
point: Vector,
query_filter: SpatialQueryFilter,
callback: impl FnMut(Entity) -> bool,
) {
self.query_pipeline
.point_intersections_callback(point, query_filter, callback)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [`ColliderAabb`]
/// that is intersecting the given `aabb`.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_aabb_intersections(spatial_query: SpatialQuery) {
/// let aabb = Collider::sphere(0.5).aabb(Vec3::ZERO, Quat::default());
/// let intersections = spatial_query.aabb_intersections_with_aabb(aabb);
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn aabb_intersections_with_aabb(&self, aabb: ColliderAabb) -> Vec<Entity> {
self.query_pipeline.aabb_intersections_with_aabb(aabb)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [`ColliderAabb`]
/// that is intersecting the given `aabb`, calling `callback` for each intersection.
/// The search stops when `callback` returns `false` or all intersections have been found.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_aabb_intersections(spatial_query: SpatialQuery) {
/// let mut intersections = vec![];
///
/// spatial_query.aabb_intersections_with_aabb_callback(
/// Collider::sphere(0.5).aabb(Vec3::ZERO, Quat::default()),
/// |entity| {
/// intersections.push(entity);
/// true
/// }
/// );
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn aabb_intersections_with_aabb_callback(
&self,
aabb: ColliderAabb,
callback: impl FnMut(Entity) -> bool,
) {
self.query_pipeline
.aabb_intersections_with_aabb_callback(aabb, callback)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [`Collider`]
/// that is intersecting the given `shape` with a given position and rotation.
///
/// ## Arguments
///
/// - `shape`: The shape that intersections are tested against represented as a [`Collider`].
/// - `shape_position`: The position of the shape.
/// - `shape_rotation`: The rotation of the shape.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_shape_intersections(spatial_query: SpatialQuery) {
/// let intersections = spatial_query.shape_intersections(
/// &Collider::sphere(0.5), // Shape
/// Vec3::ZERO, // Shape position
/// Quat::default(), // Shape rotation
/// SpatialQueryFilter::default(), // Query filter
/// );
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn shape_intersections(
&self,
shape: &Collider,
shape_position: Vector,
shape_rotation: RotationValue,
query_filter: SpatialQueryFilter,
) -> Vec<Entity> {
self.query_pipeline
.shape_intersections(shape, shape_position, shape_rotation, query_filter)
}
/// An [intersection test](spatial_query#intersection-tests) that finds all entities with a [`Collider`]
/// that is intersecting the given `shape` with a given position and rotation, calling `callback` for each
/// intersection. The search stops when `callback` returns `false` or all intersections have been found.
///
/// ## Arguments
///
/// - `shape`: The shape that intersections are tested against represented as a [`Collider`].
/// - `shape_position`: The position of the shape.
/// - `shape_rotation`: The rotation of the shape.
/// - `query_filter`: A [`SpatialQueryFilter`] that determines which colliders are taken into account in the query.
/// - `callback`: A callback function called for each intersection.
///
/// ## Example
///
/// ```
/// # #[cfg(feature = "2d")]
/// # use avian2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use avian3d::prelude::*;
/// use bevy::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn print_shape_intersections(spatial_query: SpatialQuery) {
/// let mut intersections = vec![];
///
/// spatial_query.shape_intersections_callback(
/// &Collider::sphere(0.5), // Shape
/// Vec3::ZERO, // Shape position
/// Quat::default(), // Shape rotation
/// SpatialQueryFilter::default(), // Query filter
/// |entity| { // Callback function
/// intersections.push(entity);
/// true
/// },
/// );
///
/// for entity in intersections.iter() {
/// println!("Entity: {:?}", entity);
/// }
/// }
/// ```
pub fn shape_intersections_callback(
&self,
shape: &Collider,
shape_position: Vector,
shape_rotation: RotationValue,
query_filter: SpatialQueryFilter,
callback: impl FnMut(Entity) -> bool,
) {
self.query_pipeline.shape_intersections_callback(
shape,
shape_position,
shape_rotation,
query_filter,
callback,
)
}
}