Skip to content

Commit

Permalink
Merge pull request #44 from Jondolf/sensor-colliders
Browse files Browse the repository at this point in the history
Add `Sensor` component for colliders
  • Loading branch information
Jondolf committed Jun 17, 2023
2 parents ffd7fea + 9641803 commit cb233a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/components/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ fn extract_mesh_vertices_indices(mesh: &Mesh) -> Option<VerticesIndices> {
Some((vtx, idx))
}

/// Marks a [`Collider`] as a sensor collider. Sensor colliders send collision events but
/// don't cause a collision response. This is often used to detect when something enters or leaves an area.
#[derive(Reflect, Clone, Component, Debug, Default, PartialEq, Eq)]
pub struct Sensor;

/// The Axis-Aligned Bounding Box of a collider.
#[derive(Clone, Copy, Component, Deref, DerefMut, PartialEq)]
pub struct ColliderAabb(pub Aabb);
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn penetration_constraints(
mut bodies: Query<(
RigidBodyQuery,
&Collider,
Option<&Sensor>,
Option<&CollisionLayers>,
Option<&mut CollidingEntities>,
Option<&Sleeping>,
Expand All @@ -104,8 +105,8 @@ fn penetration_constraints(

for (ent1, ent2) in broad_collision_pairs.0.iter() {
if let Ok([bundle1, bundle2]) = bodies.get_many_mut([*ent1, *ent2]) {
let (mut body1, collider1, layers1, colliding_entities1, sleeping1) = bundle1;
let (mut body2, collider2, layers2, colliding_entities2, sleeping2) = bundle2;
let (mut body1, collider1, sensor1, layers1, colliding_entities1, sleeping1) = bundle1;
let (mut body2, collider2, sensor2, layers2, colliding_entities2, sleeping2) = bundle2;

let layers1 = layers1.map_or(CollisionLayers::default(), |l| *l);
let layers2 = layers2.map_or(CollisionLayers::default(), |l| *l);
Expand Down Expand Up @@ -152,16 +153,19 @@ fn penetration_constraints(
started_collisions.push(CollisionStarted(*ent1, *ent2));
}

// When an active body collides with a sleeping body, wake up the sleeping body.
// When an active body collides with a sleeping body, wake up the sleeping body
if sleeping1.is_some() {
commands.entity(*ent1).remove::<Sleeping>();
} else if sleeping2.is_some() {
commands.entity(*ent2).remove::<Sleeping>();
}

let mut constraint = PenetrationConstraint::new(*ent1, *ent2, contact);
constraint.solve([&mut body1, &mut body2], sub_dt.0);
penetration_constraints.0.push(constraint);
// Create and solve constraint if both colliders are solid
if sensor1.is_none() && sensor2.is_none() {
let mut constraint = PenetrationConstraint::new(*ent1, *ent2, contact);
constraint.solve([&mut body1, &mut body2], sub_dt.0);
penetration_constraints.0.push(constraint);
}
} else {
let mut collision_ended_1 = false;
let mut collision_ended_2 = false;
Expand Down

0 comments on commit cb233a3

Please sign in to comment.