audio_sound_positioning - fix SetSoundPosition and panning

This commit is contained in:
TheNoiselessNoise 2026-08-07 07:47:23 +02:00
commit c6b34d2322

View file

@ -116,14 +116,21 @@ static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, flo
// Calculate normalized vectors for spatial positioning
Vector3 normalizedDirection = Vector3Normalize(direction);
Vector3 forward = Vector3Normalize(Vector3Subtract(listener.target, listener.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(listener.up, forward));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, listener.up));
// Reduce volume for sounds behind the listener
float dotProduct = Vector3DotProduct(forward, normalizedDirection);
if (dotProduct < 0.0f) attenuation *= (1.0f + dotProduct*0.5f);
// Set stereo panning based on sound position relative to listener
float pan = 0.5f + 0.5f*Vector3DotProduct(normalizedDirection, right);
float pan = 0;
if (RAYLIB_VERSION_MAJOR >= 6) {
// -1.0 - 1.0
pan = Clamp(Vector3DotProduct(normalizedDirection, right), -1.0f, 1.0f);
} else {
// 0.0 - 1.0
pan = 0.5f + 0.5f*Vector3DotProduct(normalizedDirection, right);
}
// Apply final sound properties
SetSoundVolume(sound, attenuation);