Skip to content

Commit

Permalink
Merge pull request #136 from NiklasEi/handle-zero-distance-between-em…
Browse files Browse the repository at this point in the history
…itter-and-receiver

Prevent NaN angle at zero emitter and receiver distance
  • Loading branch information
NiklasEi authored Dec 8, 2024
2 parents 1569135 + e74796d commit 2f69421
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

- fix spatial audio when position of receiver and emitter are the same ([#135](https://github.com/NiklasEi/bevy_kira_audio/issues/135))

## v0.21.0 - 30.11.2024
- Update to Bevy `0.15`

Expand Down
8 changes: 7 additions & 1 deletion src/spatial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{AudioInstance, AudioTween};
use bevy::asset::{Assets, Handle};
use bevy::ecs::component::Component;
use bevy::math::Vec3;
use bevy::prelude::{GlobalTransform, Query, Res, ResMut, Resource, With};
use std::f32::consts::PI;

/// Component for audio emitters
///
Expand Down Expand Up @@ -45,7 +47,11 @@ impl SpatialAudio {
.clamp(0., 1.)
.powi(2);

let right_ear_angle = receiver_transform.right().angle_between(sound_path);
let right_ear_angle = if sound_path == Vec3::ZERO {
PI / 2.
} else {
receiver_transform.right().angle_between(sound_path)
};
let panning = (right_ear_angle.cos() + 1.) / 2.;

for instance in emitter.instances.iter() {
Expand Down

0 comments on commit 2f69421

Please sign in to comment.