Algorithm for setting the correct "audio" volume based on distance (x, y)

I am developing a game. How can I “fade out” the sound volume for sound based on the “distance” of the sprite from the current rendering scene? Suppose I have a world:

WIDTH_WORLD = 10000
HEIGHT_WORLD = 10000

Current Scene  
xCurrent = 800   ( + Width Res. = 800 + 1024 = 1824)  
yCurrent = 400   ( + Height Res. = 400 + 768 =... )

Far Sprite
xSprite = 7000 
ySprite = 3000 

What is a good algorithm for “calculating” sound volume (and possibly left / right channel ratios)? Thanks in advance!

+5
source share
3 answers

General wave

  • There are several approaches to attenuating volume. For instance. you can use the linear damping function, for example volume = max( 0, max_volume - max_volume/max_distance * distance ), or the function with reverse removal, for example volume = min( max_volume, max_volume / distance ).

Balance ratio

  • , . , , , , , , . balance = (object_x-camera_x) / screen_half_width -1: , +1: 0: .

, : , , , -...

+2

(Dsquared = xdist ^ 2 + ydist ^ 2) , . , ( , ).

/ ( ), , . , .

+2

, , .

, , , , ​​.

0

All Articles