5
\$\begingroup\$

I'm currently working on a 2D top-down survival game and I'm trying to figure out a good method for enemies to detect player sounds. For instance - if the player fires his gun it should play a sound (already implemented) and then enemies near the player should 'hear' the sound and react to it.

The obvious solution is to simply check a radius around the sound origin for enemies and flag them as having heard the sound. The issue here is that I have many buildings and walls and other objects that would normally interfere with sound. If I simply do a radius check then enemies will hear the player through walls and bulkheads.

The next obvious solution is to use raycasting. I cast a ray to every enemy within a radius of the origin of the sound and if the ray hasn't hit anything then the enemy can hear the sound. This method works, but I'm looking for something more natural.

What I'm hoping for is a method of having sound rays penetrate walls and lose intensity depending on the object. I'm using Farseer Physics and am currently working towards this implementation but there's one catch that I haven't figured out. If an enemy is around a corner then the ray will pass through the wall and lower intensity even though the enemy SHOULD be able to hear the sound loud and clear. I know this has to do with sounds bouncing off of other walls and echoing but I have no idea how to implement that without raycasting like crazy.

My question then becomes: Does anyone have a semi-realistic method of producing a sound in a 2D world, and having enemies react to it through, and around walls?

\$\endgroup\$
  • \$\begingroup\$ Related (duplicate?): gamedev.stackexchange.com/questions/26576/… \$\endgroup\$ – MichaelHouse Apr 4 '14 at 21:16
  • \$\begingroup\$ Maybe use a pathfinding algorithm which uses different costs for different materials depending on how sound-proof they are supposed to be, calculate the best path between sound-source and sound-receiver and then use the total cost of that path as dampening factor? \$\endgroup\$ – Philipp Apr 4 '14 at 22:07
  • 2
    \$\begingroup\$ Note that a too realistic simulation of sound might be unintuitive and unpredictable from the players point of view and thus might be detrimental to the game experience. A simpler model might have a better gameplay value. \$\endgroup\$ – Philipp Apr 4 '14 at 22:12
  • \$\begingroup\$ This sounds crazy, but what about implementing the particle method from this answer? It seems to fit your requirements, somewhat. \$\endgroup\$ – ashes999 Apr 5 '14 at 2:50
  • 3
    \$\begingroup\$ What about raycasting but where each ray (that represents your sound ray) has an intensity (or volume) and that the intensity drops with 1) distance and 2) upon colliding with a wall, floor, ceiling, etc...? Like, say your sound ray starts with intensity 100, it would drop by 2 every meter and if it hits a wall it would drop by 30 or whatever. \$\endgroup\$ – TheSeamau5 Apr 5 '14 at 3:39
6
\$\begingroup\$

This is a line-of-sight problem in disguise, and if your walls are vector based you can apply mechanics typically used for realistic lighting to it. If not, you can vectorize them and apply the same algorithms.

You have sound sources and listeners, and you can think of walls as casting "sound shadows."

For refractive sound (around corners, down halls)

Sweep a ray from the sound source to each wall vertex and listener in order to find out if any listeners are between the sound source and occluding walls. For walls that are partially occluded, repeat the process starting from the ray's angle of incidence to the wall intersection and proceeding clockwise or counter-clockwise (depending on the direction you swept the original ray).

This way, the nearest walls to the source will limit the sound from passing through. You can use the partial wall behaviour above to gather listeners where sound still passes through but at a reduced volume.

See "Red Blob Games - 2d Visibility" for examples of how these algorithms can be visualized.

Design considerations

As a matter of design advice, players will bring their own expectations into the game about how sound should behave so it will be important to highlight these mechanics. Otherwise they may feel as though some sounds should have been blocked, or that there was no way they could know that a sound would echo all the way down a hall.

A bonus to approaching it this way is you also get a clean way of displaying how sound travels, either to show the player or for testing. Just create a mask from the swept areas and display it like a light source or animation, like a series of expanding rings.

\$\endgroup\$
  • \$\begingroup\$ +1 this is a really neat, simple approach (treating it like lighting / LoS) \$\endgroup\$ – ashes999 Jun 18 '14 at 17:56
0
\$\begingroup\$

Although I think I would go for patfinding to listeners if possible myself another idea would be to flood fill the are with sound sort of like a breadth first search or similar to dijkstra's with cheapest node next. Depending on your world this might or might not be computationally expensive. Maybe your audiogrid would not have to be as fine grained as everything else?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.