The hearing function using a graph-based system

Hearing works similarly to vision but doesn't take into account the nodes direct visibility because of the properties of the sound. However, we still need a sound receiver in order to make it work. Instead of making an agent a direct sound receiver, in this recipe, the sound travels along the sound graph and is perceived by the graph nodes.

Getting ready

It is important to have grasped the chapter regarding path finding in order to understand the inner workings of the graph-based recipes.

How to do it…

  1. Create the emitter class:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class EmitterGraph : MonoBehaviour
    {
        // next steps
    }
  2. Declare the member variables:
    public int soundIntensity;
    public Graph soundGraph;
    public GameObject emitterObj;
  3. Implement the validation of the emitter object's reference:
    public void Start()
    {
        if (emitterObj == null)
            emitterObj = gameObject;
    }
  4. Declare the function for emitting sounds:
    public int[] Emit()
    {
        // next steps
    }
  5. Declare and assign the variables needed:
    List<int> nodeIds = new List<int>();
    Queue<int> queue = new Queue<int>();
    List<int> neighbours;
    int intensity = soundIntensity;
    int src = soundGraph.GetNearestVertex(emitterObj);
  6. Add the source node to the list of reached nodes and the queue:
    nodeIds.Add(src);
    queue.Enqueue(src);
  7. Code the Breadth-First Search loop for reaching out to nodes:
    while (queue.Count != 0)
    {
        // next steps
    }
    return nodeIds.ToArray();
  8. Finish the loop if the sound runs out of intensity:
    if (intensity == 0)
        break;
  9. Take a node from the queue and get its neighbors:
    int v = queue.Dequeue();
    neighbours = soundGraph.GetNeighbors(v);
  10. Check the neighbors and add them to the queue if necessary:
    foreach (int n in neighbours)
    {
        if (nodeIds.Contains(n))
            continue;
        queue.Enqueue(n);
        nodeIds.Add(n);
    }
  11. Reduce the sound intensity:
    intensity--;

How it works…

The recipe returns the list of affected nodes by the sound intensity using the Breadth-First Search algorithm. The algorithm stops when there are no more nodes to visit, or when the intensity of the sound is dimmed by the graph traversal.

There is more…

After learning how to implement hearing using both colliders and graph logic, you could develop a new hybrid algorithm that relies on a heuristic that takes distance as inputs. If a node goes beyond the sound's maximum distance, there's no need to add its neighbors to the queue.

See also

The following recipes of Chapter 2, Navigation:

  • Breadth-First Search algorithm
  • A* algorithm (for taking a heuristic function as argument)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.197