Sound spatialization
Both sf::Sound
and sf::Music
also support spatial positioning. It takes advantage of left and right audio channels and makes it feel like the sound is actually playing around you. There is a catch, though. Every sound or music instance that is desired to be spatial has to only have a single channel. It is more commonly known as a monophonic or mono sound, as opposed to stereo that already decides how the speakers are used.
The way sounds are perceived in three-dimensional space is manipulated through a single, static class: sf::Listener
. It's static because there can only ever be one listener per application. The main two aspects of this class we're interested in are the position and direction of the listener. Keep in mind that although we may be working on a 2D game, SFML sounds exist in 3D space. Let's take a look at an example:
sf::Listener::setPosition(5.f, 0.f, 5.f); sf::Listener::setDirection(1.f, 0.f, 0.f);
First, let's address the three-dimensional...