Use correct transform when computing offsets.

This commit is contained in:
Nolan Darilek 2022-08-27 12:26:58 -05:00
parent 1020f818b9
commit 124cd74cb1

View File

@ -8,32 +8,16 @@ use crate::{commands::RunIfExistsExt, core::GlobalTransformExt};
pub struct Volumetric; pub struct Volumetric;
fn update( fn update(
mut commands: Commands,
listener: Query<(&Collider, &GlobalTransform), With<Listener>>, listener: Query<(&Collider, &GlobalTransform), With<Listener>>,
mut sounds: Query< mut sounds: Query<(&Sound, &Parent, &mut Transform), With<Volumetric>>,
( colliders: Query<(&Collider, &GlobalTransform)>,
Entity,
Option<&Collider>,
Option<&Parent>,
&mut Transform,
&GlobalTransform,
),
With<Volumetric>,
>,
colliders: Query<&Collider>,
) { ) {
if let Ok((listener_collider, listener_global_transform)) = listener.get_single() { if let Ok((listener_collider, listener_global_transform)) = listener.get_single() {
for (sound_entity, sound_collider, parent, mut sound_transform, sound_global_transform) in for (sound, parent, mut sound_transform) in &mut sounds {
&mut sounds if sound.paused {
{ continue;
let sound_collider = if sound_collider.is_some() { }
sound_collider if let Ok((sound_collider, sound_global_transform)) = colliders.get(**parent) {
} else if let Some(parent) = parent {
colliders.get(**parent).ok()
} else {
None
};
if let Some(sound_collider) = sound_collider {
let closest = listener_global_transform.closest_points( let closest = listener_global_transform.closest_points(
listener_collider, listener_collider,
sound_global_transform, sound_global_transform,
@ -41,18 +25,9 @@ fn update(
); );
if let ClosestPoints::WithinMargin(_p1, p2) = closest { if let ClosestPoints::WithinMargin(_p1, p2) = closest {
let p2 = Vec3::new(p2.x, p2.y, 0.); let p2 = Vec3::new(p2.x, p2.y, 0.);
if p2.distance(sound_global_transform.translation()) >= 1. { sound_transform.translation.x = p2.x - sound_global_transform.translation().x;
sound_transform.translation.x = sound_transform.translation.y = p2.y - sound_global_transform.translation().y;
p2.x - sound_global_transform.translation().x;
sound_transform.translation.y =
p2.y - sound_global_transform.translation().y;
}
} else if closest == ClosestPoints::Intersecting { } else if closest == ClosestPoints::Intersecting {
if sound_global_transform
.translation()
.distance(listener_global_transform.translation())
>= 1.
{
sound_transform.translation.x = listener_global_transform.translation().x sound_transform.translation.x = listener_global_transform.translation().x
- sound_global_transform.translation().x; - sound_global_transform.translation().x;
sound_transform.translation.y = listener_global_transform.translation().y sound_transform.translation.y = listener_global_transform.translation().y
@ -62,7 +37,6 @@ fn update(
} }
} }
} }
}
fn removed(mut commands: Commands, removed: RemovedComponents<Volumetric>) { fn removed(mut commands: Commands, removed: RemovedComponents<Volumetric>) {
for entity in removed.iter() { for entity in removed.iter() {