avian2d::collision::collider

Trait AnyCollider

Source
pub trait AnyCollider: Component<Mutability = Mutable> + ComputeMassProperties {
    type Context: for<'w, 's> ReadOnlySystemParam<Item<'w, 's>: Send + Sync>;

    // Required methods
    fn aabb_with_context(
        &self,
        position: Vector,
        rotation: impl Into<Rotation>,
        context: AabbContext<'_, '_, '_, Self::Context>,
    ) -> ColliderAabb;
    fn contact_manifolds_with_context(
        &self,
        other: &Self,
        position1: Vector,
        rotation1: impl Into<Rotation>,
        position2: Vector,
        rotation2: impl Into<Rotation>,
        prediction_distance: Scalar,
        manifolds: &mut Vec<ContactManifold>,
        context: ContactManifoldContext<'_, '_, '_, Self::Context>,
    );

    // Provided method
    fn swept_aabb_with_context(
        &self,
        start_position: Vector,
        start_rotation: impl Into<Rotation>,
        end_position: Vector,
        end_rotation: impl Into<Rotation>,
        context: AabbContext<'_, '_, '_, Self::Context>,
    ) -> ColliderAabb { ... }
}
Expand description

A trait that generalizes over colliders. Implementing this trait allows colliders to be used with the physics engine.

Required Associated Types§

Source

type Context: for<'w, 's> ReadOnlySystemParam<Item<'w, 's>: Send + Sync>

A type providing additional context for collider operations.

Context allows you to access an arbitrary ReadOnlySystemParam on the world, for context-sensitive behavior in collider operations. You can use this to query components on the collider entity, or get any other necessary context from the world.

§Example
#[derive(Component)]
pub struct VoxelCollider;

#[derive(Component)]
pub struct VoxelData {
    // collider voxel data...
}

impl AnyCollider for VoxelCollider {
    type Context = (
        // you can query extra components here
        SQuery<&'static VoxelData>,
        // or put any other read-only system param here
        SRes<Time>,
    );

    fn contact_manifolds_with_context(
        &self,
        other: &Self,
        position1: Vector,
        rotation1: impl Into<Rotation>,
        position2: Vector,
        rotation2: impl Into<Rotation>,
        prediction_distance: Scalar,
        manifolds: &mut Vec<ContactManifold>,
        context: ContactManifoldContext<Self::Context>,
    ) {
        let [voxels1, voxels2] = context.0.get_many([context.entity1, context.entity2])
            .expect("our own `VoxelCollider` entities should have `VoxelData`");
        let elapsed = context.1.elapsed();
        // do some computation...
    }
}

Required Methods§

Source

fn aabb_with_context( &self, position: Vector, rotation: impl Into<Rotation>, context: AabbContext<'_, '_, '_, Self::Context>, ) -> ColliderAabb

Computes the Axis-Aligned Bounding Box of the collider with the given position and rotation.

See SimpleCollider::aabb for collider types with empty AnyCollider::Context

The rotation is counterclockwise and in radians.

Source

fn contact_manifolds_with_context( &self, other: &Self, position1: Vector, rotation1: impl Into<Rotation>, position2: Vector, rotation2: impl Into<Rotation>, prediction_distance: Scalar, manifolds: &mut Vec<ContactManifold>, context: ContactManifoldContext<'_, '_, '_, Self::Context>, )

Computes all ContactManifolds between two colliders.

Returns an empty vector if the colliders are separated by a distance greater than prediction_distance or if the given shapes are invalid.

See SimpleCollider::contact_manifolds for collider types with empty AnyCollider::Context

Provided Methods§

Source

fn swept_aabb_with_context( &self, start_position: Vector, start_rotation: impl Into<Rotation>, end_position: Vector, end_rotation: impl Into<Rotation>, context: AabbContext<'_, '_, '_, Self::Context>, ) -> ColliderAabb

Computes the swept Axis-Aligned Bounding Box of the collider. This corresponds to the space the shape would occupy if it moved from the given start position to the given end position.

See SimpleCollider::swept_aabb for collider types with empty AnyCollider::Context

The rotation is counterclockwise and in radians.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§