avian3d::prelude

Derive Macro PhysicsLayer

source
#[derive(PhysicsLayer)]
Expand description

A derive macro for defining physics layers using an enum.

Each variant of the enum represents a layer. Each layer has a unique bit determined by the order of the variants. The bit value can be retrieved using the to_bits method.

§Requirements

  • The enum must have at most 32 variants.
  • The enum variants must not have any fields.
  • The enum must have a default variant with the #[default] attribute.
    • The first bit 1 << 0 will always be reserved for the default layer. The bit values of the other layers are determined by their order in the enum, starting from 1 << 1.

§Example

#[derive(PhysicsLayer, Clone, Copy, Debug, Default)]
enum GameLayer {
    #[default]
    Default, // Layer 0 - the default layer that objects are assigned to
    Player,  // Layer 1
    Enemy,   // Layer 2
    Ground,  // Layer 3
}

// The first bit is reserved for the default layer.
assert_eq!(GameLayer::default().to_bits(), 1 << 0);

// The `GameLayer::Ground` layer is the fourth layer, so its bit value is `1 << 3`.
assert_eq!(GameLayer::Ground.to_bits(), 1 << 3);