[Unity3D] - A simple audio engine for physics rigid bodies


Today, we want to share with you the simple audio engine we made for our new game Boliscape - the ball escape game. Our engine manage all the physics sound for collisions and prolonged contacts between rigid bodies in our game. We will not cover the code in detail but we will make the code available to you (2 files with a total of ~500 lines of code). We will also cover how you can easily integrate this engine in your unity game! And good news! The code comes with NO LICENSE AT ALL. Feel free to do whatever you want with it for your commercial and non-commercial games.

Before we procceed to the presentation of our simple engine let's contextualize why we had to make a custom audio engine for physics sounds in our game. As you may know, Unity comes with a great general purpose 3D physics engine called PhysX. PhysX allows you to basically simulate everything you want with rigid bodies. However, there is no tool include in unity that allows you out-of-the-box quickly manage the natural sounds that result from the physical interactions between the involved rigid bodies. There is however some assets in the unity asset store that do that but they are mostly paid assets and we found out to be hard to get started with.

Our solution is a simple approach that can be seen as an (gross?) approximation of what would be the physics sounds in real life but we are very happy of the result for our game and we believe this solution is suitable for most games that need a simple audio engine for physics sounds. Also, we found out the performance to be great for a mobile game as is the case for our game Boliscape.

Before we proceed to the integration of the engine in your unity project, let's first summarize the main characteristics of the engine below:

1 - The engine divides physics sounds in two classes: collision sounds and contact sounds.

2 - A collision sound can be seen as a very short sound that you would hear when two objets collide and go appart quickly from each other. An example of a collision sound is a basket ball bouncing on the floor: the collision sound does even last half a second. You can expect the clip to be played with higher volume and higher pitch when the collision velocity increase. Also, the engine will fade out the collision audio clip when it has almost finished playing.

3 - A contact sound can be seen as a sound that plays continuously when two rigid bodies are in contact for a prolonged amount of time. During a contact, the objects keep touching each other for a given amount of time and generally one is sliding or rolling into the other one. An example of a contact sound is someone pushing a wooden box that sits on the floor. You can expect an increase or decrease of both volume and pitch whether the person is pushing the box faster or slower, respectively.

4 - For reasons related to performance, the engine uses a pooling mechanism to store the unity audio sources used by the engine. This means, that after the engine considers that a collision or a contact does not need an audio response anymore, the audio source used for that collision or contact will return to the pool and marked as available for any future physics interaction that requires an audio output. This helps keeping the number of instantiated unity audio sources in our scene low.

With that being said, let's see/hear an example of a collision from our game Boliscape:

Now, an example of contact:

And finally collision and contact altogether:


Now, let's move to the part you are all waiting for: how to integrate this engine in your project. The integration is quite easy:


1 - You need to add script AudioTrigger as a component for every rigid body you wish physics sound to be played. If you are generating  meshes at runtime, you can attach this script to your objects at runtime! Let's see below a print of how this component looks in the inspector:

As you can see above, by setting fields WantContactAudio and WantCollisionAudio you can specify whether you desire collision, contact or both to be played for the considered rigid body. PhysicsMaterialName is the name of the physics material used by the rigid body for contact purposes. This field is needed since the rigid body considered can have multiple colliders with different physics material and our engine limits contacts audio to one physics material per rigid body.


2 - You need to attach script AudioManager to ONE (only) empty game object that has to be always enable during the gameplay. Let's see below a print of how this component looks in the inspector:

Let's explore together each field of this component. Field CollisionFadeOutDuration allows you to specify the duration of the fade out for all collisions. A lower CollisionFadeOutDuration means that the collision audio will sounds to stop more abruptly.
Then, you have a list AudioClipEntries where you specify collision and contact settings for each physics material you use in your physics simulation. In this example, you can see at index 1 the settings we use in Boliscape about wood material. The other entries of the list contains the other materials we use in our scene, such as: metal, glass, rubber, etc. 

Let's consider in detail the example of WoodMaterial. Field AssociatedPhysicsMaterial represents the physics material in question. CollisionAudioClip is the one-shot short audio clip you want to be played when a wooden rigid body collide with any other rigid body. CollisionMapMinRelativeVelocityMagnitude and CollisionMapMinRelativeVelocityMagnitude specify the bounds for the relative velocity at the time of the collision. In a collision, if the relative velocity between the two bodies is less CollisionMapMinRelativeVelocityMagnitude, then no collision audio will be played, since an audio output is considered to be irrelevant. On the other hand, if the relative velocity between the two bodies is higher than CollisionMapMaxRelativeVelocityMagnitude, then the relative velocity used by the engine for collision audio calculations purpose will be equal to CollisionMapMaxRelativeVelocityMagnitude. CollisionMapMinPitch, CollisionMapMaxPitch, CollisionMapMinVolumeCollisionMapMaxVolume will be used in the engine interpolation calculations to compute the target volume and target pitch considering the retained relative velocity of the collision (considering what we have just discussed before about the bounds). CollisionFadeOutStartingTime can be used to specify when the clip should start to fade out. In the example above, the clip will start to fadeout after being played for 0.3 seconds.

ContactAudioClip is the clip you want to hear during a prolonged contact. Notice that since the clip will be played in loop, your clip needs to sound seamless (you can easily find on internet on how to make a audio clip seamless with Audacity). The remaining fields of this list entry related to contacts are similar to what we have just covered about collisions.

During a prolonged contact between two bodies, it is not desired that the volume nor the pitch changes abruptly (it would sound unatural and uncomfortable to the listener). That's why we have fields VolumeChangeVelocityOnContact and PitchChangeVelocityOnContact. These fields control how slow or fast the volume and pitch are changing towards the direction of the computed target pitch and volume. Lower values for VolumeChangeVelocityOnContact and PitchChangeVelocityOnContact means the changes over time happen slower making them sound smoothier.

That's all we have to share with you about our audio engine for rigid bodies!
We hope this article was helpful to you. If you want to support us and you want to see more articles coming out in the following days about the development of Boliscape, we just ask you kindly to download our game in google play :) If you want to have access to the collisions and contact audio clips we used for our physics material, please send us an email first.

Links to the code below:

AudioTrigger.cs => https://1drv.ms/u/s!AiSRRDHwssaags0yLtSh7cJvfBEAmg?e=cXsyY

AudioManager.cs => https://1drv.ms/u/s!AiSRRDHwssaags01UrIkH8Qt_dSd5A?e=EFlGcd

Files

https://play.google.com/store/apps/details?id=com.xelart_game_studio.boliscape
External
Sep 16, 2020

Get Boliscape - the ball escape game

Leave a comment

Log in with itch.io to leave a comment.