How to Calculate the distance of a sound source?

BlitzMax Forums/BlitzMax Beginners Area/How to Calculate the distance of a sound source?

Takis76(Posted 2014) [#1]
Hello,

I would like to create some 3D sounds using sound pan and sound volume functions.

In my example I have one door which is the sound source and this door is in some coordinates on the map.

The door have its position on the map and the party of players have their position on the map too.

The map is a grid of squares and in this example we assume the door is at the position (x,y)= 10,10.
The party if is near to the door will hear the sound louder and if the party is far will hear the door more quietly.

But the party can hear the sound at the left or at the right of the sound source if it is direction is opposite of the door.

For example if the party position (x,y) = 10,11 means the party is in front of the door in distance 1 square and will hear the sound very loudly but the sound pan will be equal (0)
If the party strife to the left to the square 9,11 then it must hear the sound panned to the right.

How Do I calculate the distance of the party x.y from the door x.y to play my sound louder or quietly or panned to the left or right according the the party's and door position. For example if the door is at (10,10) and party is at (5,7). The distance calculation will be the same regardless if the party is in front of the door if see the door behind.

I use types for my door and my party.

Party.x
Party.y

Door[lvl,n].x
Door[lvl,n].y

The "lvl" and "n" are the variables for level and door number. Both are just types.
There will be a lots of doors so I will put them in a loop to check if there are a lots of doors or any sound source near or far to calculate the distance.

Thank you :)


GfK(Posted 2014) [#2]
This might help:

//get distance
dx# = door[lvl,n].x - Party.x
dy# = door[lvl,n].y - Party.y

distance# = Sqr((dx*dx)+(dy*dy));

//get direction
direction# = Atan2(door[lvl,n].y - Party.y, door[lvl,n].x - Party.x)



Derron(Posted 2014) [#3]
Instead of "distance" you should do this:

- calculate amount of "walls" between you and the source
- each wall cuts by X, each door in a wall lowers the cut
- direction of the source is depending on the doors you have in your current "room"


bye
Ron


Takis76(Posted 2014) [#4]
Your code was PERFECT. Thank you very very much.


Matty(Posted 2014) [#5]
There are some interesting features you can do with sounds...

Some sounds do indeed come from a point source and so a radius squared fall off if appropriate.

But some sounds may come from different shaped sources. So for example sound which comes from a line source rather than a point would fall off directly proportional to the distance, not the square of the distance....

For example if you wanted to make the sound of the ocean while standing on the shore of the beach you would reduce the volume of the sound directly with the distance between you and the ocean. Whereas if you wanted to make the sound of a gunshot then the fall off would be related to the square of the distance....


Derron(Posted 2014) [#6]
All of this sound-distance-calculations totally ignore barriers between you and the object.

rooms redirect sounds to doors (+ some "unclear" sound through the walls...damped).


bye
Ron


Matty(Posted 2014) [#7]
If you want to get complicated then I suppose you can take into consideration diffraction of the sound waves around objects depending on wavelength/frequency of sound as well as setting up interference patterns.....but that might be going too far. ....unless you're making a rock concert simulation. ;-)


Derron(Posted 2014) [#8]
The original question was something containing doors...

It is not that hard to check the way "using doors" from source to target ... and you then could use the "shortest" (aka most powerful) way to play sound from ...


bye
Ron


Matty(Posted 2014) [#9]
Oh...Oops I kind of missed the bits about doors. In a way you could almost use your pathfinding routine to evaluate this....kill two birds with one stone.


Takis76(Posted 2014) [#10]
I with the code GfK gave , I managed to make my door (The source of the sound) it will not be and monster or spell or any kind of thing too. When you open the door you hear the door sound near and far and left and right according to the position of the player.
I haven't thought about cutting the sound. If the source is behind an obstacle like wall.
But the player can hear the sound up to 5 squares away, so even if there is an obstacle or wall the player will hear the sound quietly and far. Cutting the sound completely ruins the mystery and curiosity of what sound you hear and the atmosphere of a monster larking behind a secret passage.
For now I managed to do the 3D sound effect.