Chapter 15. Sound Spatialization and HUD

In this chapter, we will be adding all the sound effects and the HUD. We have done this in both of the previous projects, but we will do things a bit differently this time. We will explore the concept of sound spatialization and how SFML makes this otherwise complicated concept nice and easy; in addition, we will build a HUD class to encapsulate the code that draws information to the screen.

We will complete these tasks in the following order:

  • What is spatialization?
  • How SFML handles spatialization
  • Building a SoundManager class
  • Deploying emitters
  • Using the SoundManager class
  • Building a HUD class
  • Using the HUD class

What is Spatialization?

Spatialization is the act of making something relative to the space it is a part of, or within. In our daily lives, everything in the natural world, by default, is spatialized. If a motorbike whizzes past from left to right we will hear the sound grow from faint to loud from one side and as it passes by, it will become more prominent in the other ear, before fading into the distance once more. If we woke up one morning and the world was no longer spatialized, it would be exceptionally weird.

If we can make our video games a little bit more like the real world, our players can become more immersed. Our zombie game would have been a lot more fun if the player could have heard them faintly in the distance and their inhuman wailing grew louder as they drew closer, from one direction or another.

It is probably obvious that the mathematics of spatialization will be complex. How do we calculate how loud a given sound will be in a specific speaker, based on the direction the sound is coming from, and the distance from the player (the hearer of the sound) to the object that is making the sound (the emitter)?

Fortunately, SFML does all the complicated stuff for us. All we need to do is get familiar with a few technical terms and then we can start using SFML to spatialize our sound effects.

Emitters, attenuation, and listeners

We will need to be aware of a few pieces of information in order to give SFML what it needs to do its work. We will need to be aware of where the sound is coming from in our game world. This source of the sound is called an emitter. In a game, the emitter could be a Zombie, a vehicle, or in the case of our current project, a fire tile. We already keep track of the position of objects in our game, so giving SFML the emitter location will be quite straightforward.

The next factor we need to be aware of is attenuation. Attenuation is the rate at which a wave deteriorates. You could simplify that statement and make it specific to sound by saying that attenuation is how quickly the sound reduces in volume. That isn't technically exact, but it is a good enough description for the purposes of this chapter.

The final factor we need to consider is the listener. When SFML spatializes the sound, where is it spatializing it relative to? In most games, the logical thing to do is use the player character. In our game, we will use Thomas.

..................Content has been hidden....................

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