Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplified ground detection #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,5 +369,5 @@ pub struct GroundDetection {
#[derive(Component)]
pub struct GroundSensor {
pub ground_detection_entity: Entity,
pub intersecting_ground_entities: HashSet<Entity>,
// pub intersecting_ground_entities: HashSet<Entity>, // probably not needed anymore
}
43 changes: 13 additions & 30 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ pub fn spawn_ground_sensor(
.insert(GlobalTransform::default())
.insert(GroundSensor {
ground_detection_entity: entity,
intersecting_ground_entities:
HashSet::new(),
// intersecting_ground_entities:
// HashSet::new(), // probably not needed anymore
});
},
);
Expand All @@ -484,39 +484,22 @@ pub fn spawn_ground_sensor(

pub fn ground_detection(
mut ground_detectors: Query<&mut GroundDetection>,
mut ground_sensors: Query<(Entity, &mut GroundSensor)>,
mut collisions: EventReader<CollisionEvent>,
ground_sensors: Query<(Entity, &GroundSensor)>,
rapier_context: Res<RapierContext>,
// rigid_bodies: Query<&RigidBody>,
) {
for (entity, mut ground_sensor) in
ground_sensors.iter_mut()
{
for collision in collisions.iter() {
match collision {
CollisionEvent::Started(a, b, _) => {
if a == &entity {
ground_sensor
.intersecting_ground_entities
.insert(*b);
}
}
CollisionEvent::Stopped(a, b, _) => {
if a == &entity {
ground_sensor
.intersecting_ground_entities
.remove(&b);
}
}
for (entity, ground_sensor) in ground_sensors.iter() {
let mut intersection = false;
for (_collider1, _collider2, intersecting) in rapier_context.intersections_with(entity) {
if intersecting {
intersection = intersecting;
}
}

if let Ok(mut ground_detection) = ground_detectors
.get_mut(ground_sensor.ground_detection_entity)
.get_mut(ground_sensor.ground_detection_entity)
{
ground_detection.on_ground = ground_sensor
.intersecting_ground_entities
.len()
> 0;
ground_detection.on_ground = intersection;
}
}
}

}