Game Development Reference
In-Depth Information
angle needs to be in radians for the trigonometric functions to work. That's why we trans-
form the angle from degrees to radians using the formula:
The final property which can be set to the
Listener
global volume. This can be done
by calling
Listener::setGlobalVolume()
. The function expects
float
in the
range [0…100]. It combines that value with the volume from each individual sound to cal-
culate the final volume.
To finish the code for the listener, there are
Listener::get*()
functions for each of
the properties covered in this section.
Audio sources
We covered the first part of the sound environment—the
Listener
actor, which is re-
sponsible for
listening
to the sounds being played in the environment. However, without
any sounds being played, there is little point in having
Listener
. In this section, we
will explore the functionality of the
SoundSource
class with regards to sound spatializ-
ation.
Every mono
SoundSource
is spatialized by default. If we do not touch any of the prop-
erties of
Listener
and
SoundSource
they remain at the same place, and it will ap-
pear that the sources play at full volume. However, once we start moving
Listener
from the origin (0; 0; 0), we start getting sounds which are fading away and disappearing.
This is not ideal if we just want to play a non-spatialized sound. We will explore ways of
dealing with this issue in this section.
The most important property of any
SoundSource instance
is its position. The pos-
ition is the main factor which determines how loud and from which direction the source
will be played. We can set the position of
SoundSource
by calling
SoundSource::setPosition()
. This is very similar to the
Listener
class as it
expects a 3D vector. We will use the
z = 0
convention here as well: