impl Group

This commit is contained in:
Tristan Guichaoua
2022-08-13 14:31:27 +02:00
parent 68d250f0ad
commit 6c2eca72a5
4 changed files with 95 additions and 13 deletions

View File

@@ -24,8 +24,8 @@ pub fn init_world(testbed: &mut Testbed) {
/* /*
* Setup groups * Setup groups
*/ */
const GREEN_GROUP: InteractionGroups = InteractionGroups::new(0b01, 0b01); const GREEN_GROUP: InteractionGroups = InteractionGroups::new(Group::GROUP_1, Group::GROUP_1);
const BLUE_GROUP: InteractionGroups = InteractionGroups::new(0b10, 0b10); const BLUE_GROUP: InteractionGroups = InteractionGroups::new(Group::GROUP_2, Group::GROUP_2);
/* /*
* A green floor that will collide with the GREEN group only. * A green floor that will collide with the GREEN group only.

View File

@@ -24,8 +24,8 @@ pub fn init_world(testbed: &mut Testbed) {
/* /*
* Setup groups * Setup groups
*/ */
const GREEN_GROUP: InteractionGroups = InteractionGroups::new(0b01, 0b01); const GREEN_GROUP: InteractionGroups = InteractionGroups::new(Group::GROUP_1, Group::GROUP_1);
const BLUE_GROUP: InteractionGroups = InteractionGroups::new(0b10, 0b10); const BLUE_GROUP: InteractionGroups = InteractionGroups::new(Group::GROUP_2, Group::GROUP_2);
/* /*
* A green floor that will collide with the GREEN group only. * A green floor that will collide with the GREEN group only.

View File

@@ -18,14 +18,14 @@
#[repr(C)] #[repr(C)]
pub struct InteractionGroups { pub struct InteractionGroups {
/// Groups memberships. /// Groups memberships.
pub memberships: u32, pub memberships: Group,
/// Groups filter. /// Groups filter.
pub filter: u32, pub filter: Group,
} }
impl InteractionGroups { impl InteractionGroups {
/// Initializes with the given interaction groups and interaction mask. /// Initializes with the given interaction groups and interaction mask.
pub const fn new(memberships: u32, filter: u32) -> Self { pub const fn new(memberships: Group, filter: Group) -> Self {
Self { Self {
memberships, memberships,
filter, filter,
@@ -34,22 +34,22 @@ impl InteractionGroups {
/// Allow interaction with everything. /// Allow interaction with everything.
pub const fn all() -> Self { pub const fn all() -> Self {
Self::new(u32::MAX, u32::MAX) Self::new(Group::ALL, Group::ALL)
} }
/// Prevent all interactions. /// Prevent all interactions.
pub const fn none() -> Self { pub const fn none() -> Self {
Self::new(0, 0) Self::new(Group::NONE, Group::NONE)
} }
/// Sets the group this filter is part of. /// Sets the group this filter is part of.
pub const fn with_memberships(mut self, memberships: u32) -> Self { pub const fn with_memberships(mut self, memberships: Group) -> Self {
self.memberships = memberships; self.memberships = memberships;
self self
} }
/// Sets the interaction mask of this filter. /// Sets the interaction mask of this filter.
pub const fn with_filter(mut self, filter: u32) -> Self { pub const fn with_filter(mut self, filter: Group) -> Self {
self.filter = filter; self.filter = filter;
self self
} }
@@ -60,7 +60,10 @@ impl InteractionGroups {
/// with the filter of `rhs`, and vice-versa. /// with the filter of `rhs`, and vice-versa.
#[inline] #[inline]
pub const fn test(self, rhs: Self) -> bool { pub const fn test(self, rhs: Self) -> bool {
(self.memberships & rhs.filter) != 0 && (rhs.memberships & self.filter) != 0 // NOTE: since const ops is not stable, we have to convert `Group` into u32
// to use & operator in const context.
(self.memberships.bits() & rhs.filter.bits()) != 0
&& (rhs.memberships.bits() & self.filter.bits()) != 0
} }
} }
@@ -69,3 +72,82 @@ impl Default for InteractionGroups {
Self::all() Self::all()
} }
} }
use bitflags::bitflags;
bitflags! {
/// A bit mask identifying groups for interaction.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Group: u32 {
/// The group n°1.
const GROUP_1 = 1 << 0;
/// The group n°2.
const GROUP_2 = 1 << 1;
/// The group n°3.
const GROUP_3 = 1 << 2;
/// The group n°4.
const GROUP_4 = 1 << 3;
/// The group n°5.
const GROUP_5 = 1 << 4;
/// The group n°6.
const GROUP_6 = 1 << 5;
/// The group n°7.
const GROUP_7 = 1 << 6;
/// The group n°8.
const GROUP_8 = 1 << 7;
/// The group n°9.
const GROUP_9 = 1 << 8;
/// The group n°10.
const GROUP_10 = 1 << 9;
/// The group n°11.
const GROUP_11 = 1 << 10;
/// The group n°12.
const GROUP_12 = 1 << 11;
/// The group n°13.
const GROUP_13 = 1 << 12;
/// The group n°14.
const GROUP_14 = 1 << 13;
/// The group n°15.
const GROUP_15 = 1 << 14;
/// The group n°16.
const GROUP_16 = 1 << 15;
/// The group n°17.
const GROUP_17 = 1 << 16;
/// The group n°18.
const GROUP_18 = 1 << 17;
/// The group n°19.
const GROUP_19 = 1 << 18;
/// The group n°20.
const GROUP_20 = 1 << 19;
/// The group n°21.
const GROUP_21 = 1 << 20;
/// The group n°22.
const GROUP_22 = 1 << 21;
/// The group n°23.
const GROUP_23 = 1 << 22;
/// The group n°24.
const GROUP_24 = 1 << 23;
/// The group n°25.
const GROUP_25 = 1 << 24;
/// The group n°26.
const GROUP_26 = 1 << 25;
/// The group n°27.
const GROUP_27 = 1 << 26;
/// The group n°28.
const GROUP_28 = 1 << 27;
/// The group n°29.
const GROUP_29 = 1 << 28;
/// The group n°30.
const GROUP_30 = 1 << 29;
/// The group n°31.
const GROUP_31 = 1 << 30;
/// The group n°32.
const GROUP_32 = 1 << 31;
/// All of the groups.
const ALL = u32::MAX;
/// None of the groups.
const NONE = 0;
}
}

View File

@@ -8,7 +8,7 @@ pub use self::contact_pair::{
pub use self::interaction_graph::{ pub use self::interaction_graph::{
ColliderGraphIndex, InteractionGraph, RigidBodyGraphIndex, TemporaryInteractionIndex, ColliderGraphIndex, InteractionGraph, RigidBodyGraphIndex, TemporaryInteractionIndex,
}; };
pub use self::interaction_groups::InteractionGroups; pub use self::interaction_groups::{Group, InteractionGroups};
pub use self::narrow_phase::NarrowPhase; pub use self::narrow_phase::NarrowPhase;
pub use self::collider::{Collider, ColliderBuilder}; pub use self::collider::{Collider, ColliderBuilder};