3D sound – spatialization
Now let's look at ways to create some 3D audio to bring depth to a game scene. When we walk past a torch, we want to hear it move past us, and we want to be able to hear our enemies coming at us from a direction. Spatialization allows us to do this, and SFML has great features to help us achieve that.
The audio listener
We've already defined what the audio listener is and how it is used to create spatialized audio. As the first step toward achieving this, we need to set the position of the listener after each update, ensuring that all the sounds in the level are heard from the player's perspective.
At the start of each game's update, we recalculate the player's position. Right after this we can update the position of the listener class to this new location. Remember that sf::Listener
is a static class and we don't instantiate it. All that we need to do is make a static call to sf::Listener::setPosition
.
Let's append this to the Game::Update
function, as follows:
// Update...