I am using bevy_rapier2d for collision detection between entities with unique components, i.e. Player and Enemy. I want certain behavior when players and enemies collide - however, it's unclear to me the ordering of entities in CollisionEvent. For example:
fn player_enemy_collisions(
mut commands: Commands,
mut collision_er: EventReader<CollisionEvent>,
mut damange_ew: EventWriter<DamageEvent>,
query: Query<Entity, With<Enemy>>,
player: Query<Entity, With<Player>>
) {
for collision_event in collision_er.read() {
match collision_event {
CollisionEvent::Started(entity1, entity2, _flags) => {
if let Ok(_entity) = player.get(*entity1){
if let Ok(_transform) = query.get(*entity2){
damange_ew.send(DamageEvent {
entity: *entity1,
damage: 20.0, // todo: eventual damage calculations with uncertainty
});
commands.entity(*entity2).insert(Destroy); // todo: add this component, or raise an event?
}
}
}
CollisionEvent::Stopped(_entity1, _entity2, _flags) => {
// println!("Collision stopped between {:?} and {:?}", entity1, entity2);
}
}
}
}
This system assumes that the Player component is associated with the first entity, but this is not always true. How do I deal with this? Do I just double up the logic to check both permutations of entity1 and entity2? Thanks for the help.