avian3d/collision/collider/
world_query.rs

1// This is at the module-level because `QueryData` generates a `ColliderQueryItem` type,
2// and we cannot easily document or allow missing docs for that.
3#![allow(missing_docs)]
4
5use crate::prelude::*;
6use bevy::{ecs::query::QueryData, prelude::*};
7
8/// A [`QueryData`] struct to make code handling colliders cleaner.
9///
10/// This is mostly an internal type, but can also be used by users.
11#[derive(QueryData)]
12pub struct ColliderQuery<C: AnyCollider> {
13    pub entity: Entity,
14    pub of: Option<&'static ColliderOf>,
15    pub position: Ref<'static, Position>,
16    pub rotation: Ref<'static, Rotation>,
17    pub accumulated_translation: Option<Ref<'static, AccumulatedTranslation>>,
18    pub transform: Option<&'static ColliderTransform>,
19    pub aabb: Ref<'static, ColliderAabb>,
20    pub collision_margin: Option<&'static CollisionMargin>,
21    pub speculative_margin: Option<&'static SpeculativeMargin>,
22    pub is_rb: Has<RigidBody>,
23    pub is_sensor: Has<Sensor>,
24    pub collision_events_enabled: Has<CollisionEventsEnabled>,
25    pub friction: Option<&'static Friction>,
26    pub restitution: Option<&'static Restitution>,
27    pub shape: &'static C,
28    pub layers: &'static CollisionLayers,
29    pub active_hooks: Option<&'static ActiveCollisionHooks>,
30}
31
32impl<C: AnyCollider> ColliderQueryItem<'_, C> {
33    /// Returns the entity of the rigid body that the collider is attached to.
34    ///
35    /// If the collider is not attached to a rigid body, this will return `None`.
36    #[inline(always)]
37    pub fn body(&self) -> Option<Entity> {
38        self.of.map(|ColliderOf { body }| *body)
39    }
40
41    /// Returns the current position of the body. This is a sum of the [`Position`] and
42    /// [`AccumulatedTranslation`] components.
43    pub fn current_position(&self) -> Vector {
44        self.position.0
45            + self
46                .accumulated_translation
47                .as_ref()
48                .map_or_else(default, |t| t.0)
49    }
50
51    /// Returns the [`ActiveCollisionHooks`] for the collider.
52    pub fn active_hooks(&self) -> ActiveCollisionHooks {
53        self.active_hooks
54            .map_or(ActiveCollisionHooks::empty(), |h| *h)
55    }
56}