Friday, February 3, 2017

game mechanics - How to add a sound that an enemy AI can hear?


Given:



  • a 2D top down game

  • Tiles are stored just in a 2D array

  • Every tile has a property - dampen (so bricks might be -50db, air might be -1)


From this I want to add it so a sound is generated at point x1, y1 and it "ripples out". The image below kind of outlines it better. Obviously the end goal is that the AI enemy can "hear" the sound - but if a wall is blocking it, the sound doesn't travel as far.


enter image description here



Red is the wall, which has a dampen of 50db.


I think in the 3rd game tick I am confusing my maths.


What would be the best way of implementing this?



Answer



It seems like a reasonable idea, do remember though, this is a gameplay feature, don't make it more complicated than what is required for gameplay.


I'd change your scheme to have the sound spread immediately, as that is probably easier to program and seems more consistent with the fast spread of real sound.


This is essentially a pathfinding problem, and it's probably best solved using Dijkstra's algorithm. This a one to many point search (one sound source, multiple enemies), and can as such be solved efficiently by starting at the single point.


You start out by doing a spread from the source, and mark all neighbours that have not yet been marked and have a calculated volume above 0, each of those neighbours you add to a list. That list must be sorted by calculated volume. Then you repeat the process for the highest volume entry on the list, adding new entries to the list as necessary, and removing the one you have handled. Repeat until the list is empty.


Whenever you during this process reach a tile with an enemy you know what volume that enemy hears.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...