Fix audio sources using local positions instead of global

This commit is contained in:
tovjemam 2026-06-14 00:59:28 +02:00
parent 31330b9cfe
commit 8ef4677243
6 changed files with 15 additions and 14 deletions

View File

@ -3,10 +3,10 @@
audio::Player::Player(Master& master) : master_(master) {} audio::Player::Player(Master& master) : master_(master) {}
audio::SoundSource* audio::Player::PlaySound(const std::shared_ptr<const Sound>& sound, audio::SoundSource* audio::Player::PlaySound(const std::shared_ptr<const Sound>& sound,
const glm::vec3* attach_position) const game::TransformNode* attach_node)
{ {
SoundSource* source = new SoundSource(this, sound); SoundSource* source = new SoundSource(this, sound);
source->AttachToPosition(attach_position); source->AttachToNode(attach_node);
return source; return source;
} }

View File

@ -14,7 +14,7 @@ public:
Player(Master& master); Player(Master& master);
DELETE_COPY_MOVE(Player) DELETE_COPY_MOVE(Player)
SoundSource* PlaySound(const std::shared_ptr<const Sound>& sound, const glm::vec3* attach_position); SoundSource* PlaySound(const std::shared_ptr<const Sound>& sound, const game::TransformNode* attach_node);
void Update(); void Update();

View File

@ -47,12 +47,11 @@ void audio::Source::SetVelocity(const glm::vec3& velocity)
alSource3f(source_, AL_VELOCITY, velocity.x, velocity.y, velocity.z); alSource3f(source_, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
} }
void audio::Source::AttachToPosition(const glm::vec3* position) void audio::Source::AttachToNode(const game::TransformNode* node)
{ {
attach_position_ = position; attach_node_ = node;
if (attach_position_) if (attach_node_)
SetPosition(*attach_position_); SetPosition(attach_node_->GetGlobalPosition());
// TsrDebugf(DML_2, "Attached source %p to position %p\n", this, position);
} }
void audio::Source::SetRelativeToListener(bool relative) void audio::Source::SetRelativeToListener(bool relative)
@ -71,8 +70,8 @@ void audio::Source::SetRelativeToListener(bool relative)
void audio::Source::Update() void audio::Source::Update()
{ {
if (attach_position_) if (attach_node_)
SetPosition(*attach_position_); SetPosition(attach_node_->GetGlobalPosition());
} }
void audio::Source::Delete() void audio::Source::Delete()

View File

@ -3,6 +3,8 @@
#include "master.hpp" #include "master.hpp"
#include <memory> #include <memory>
#include "game/transform_node.hpp"
namespace audio namespace audio
{ {
@ -24,7 +26,7 @@ public:
void SetPosition(const glm::vec3& position); void SetPosition(const glm::vec3& position);
void SetVelocity(const glm::vec3& velocity); void SetVelocity(const glm::vec3& velocity);
void AttachToPosition(const glm::vec3* position); void AttachToNode(const game::TransformNode* node);
void SetRelativeToListener(bool relative); void SetRelativeToListener(bool relative);
virtual void SetLooping(bool looping) = 0; virtual void SetLooping(bool looping) = 0;
@ -45,7 +47,7 @@ protected:
unsigned int source_ = 0; unsigned int source_ = 0;
const glm::vec3* attach_position_ = nullptr; const game::TransformNode* attach_node_ = nullptr;
bool should_play_ = true; // auto play when created bool should_play_ = true; // auto play when created
bool finished_ = false; bool finished_ = false;
bool delete_on_finish_ = true; // auto delete when finished bool delete_on_finish_ = true; // auto delete when finished

View File

@ -103,7 +103,7 @@ bool game::view::EntityView::ProcessPlaySoundMsg(net::InMessage& msg)
return true; // dont play if not loaded yet return true; // dont play if not loaded yet
auto sound = assets::CacheManager::GetSound("data/" + std::string(name) + ".snd"); auto sound = assets::CacheManager::GetSound("data/" + std::string(name) + ".snd");
auto snd = audioplayer_.PlaySound(sound, &root_.local.position); auto snd = audioplayer_.PlaySound(sound, &root_);
snd->SetVolume(volume); snd->SetVolume(volume);
snd->SetPitch(pitch); snd->SetPitch(pitch);

View File

@ -387,7 +387,7 @@ void game::view::VehicleView::UpdateSounds()
if (accel && !snd_accel_src_) if (accel && !snd_accel_src_)
{ {
snd_accel_src_ = audioplayer_.PlaySound(snd_accel_, &root_.local.position); snd_accel_src_ = audioplayer_.PlaySound(snd_accel_, &root_);
snd_accel_src_->SetLooping(true); snd_accel_src_->SetLooping(true);
} }
else if (!accel && snd_accel_src_) else if (!accel && snd_accel_src_)