Game Development Reference
In-Depth Information
The example in the preceding image looks quite similar to the
Listener
example, with
the important difference that there can be multiple sounds in the world, and thus each
sound's position needs to be set separately. As a side note, in this example, the sound is
not linked to the sprite in any way, and we need to make sure to update the position of
growl
each time the sprite moves to keep the sound coming from the correct place.
Also, we can set a
boolean
which determines whether the position is relative to the posi-
tion of
Listener
. By default, the position of each sound is absolute, but we can make it
relative to
Listener
by calling
SoundSource::setRelativeToListener()
.This is useful when we want to
play a sound at full volume (footsteps, cloth rustling, gunshots, and so on). To do that, we
just place the sound at the origin (0; 0; 0), and set it relative to the listener. This always
plays the sound on top of the listener.
Apart from the position, each
SoundSource
has a property called
minimum distance
.
This represents the furthest distance from the listener, from which the sound can be heard
at its maximum volume. For example, if we have a sound that is 40 units away from the
listener and it has a minimum distance of 45, it will be played at full volume. However, if
the source has a minimum distance lower than 40, it will be played with a faded volume,
which depends on the attenuation factor (discussed in the following paragraph). We can
set the minimum distance of
SoundSource
by calling
SoundSource::setMinDistance()
. It expects a
float
parameter, which repres-
ents the distance in world coordinates (the value of
0
is forbidden). The default value of
the minimum distance is 1.
The final property that we can set to a sound source is attenuation. Attenuation is the
factor which determines how fast the sound fades away when it is beyond its minimal dis-
tance from the listener. For example, a factor of 1 fades the sound very slowly whereas,