Playing Sounds on Collisions
The example below demonstrates how to:
- Create a new sound source.
- Update settings of the existing sound source.
- Implement a contact callback that plays a sound at a contact with a physical body.
The sound file used in the example can be found in the <UnigineSDK>/data/samples/sounds/sounds folder. You can also specify any other file, including .mp3.
AppWorldLogic.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// World logic, it takes effect only when the world is loaded.
// These methods are called right after corresponding world script's (UnigineScript) methods.
public struct ContactArguments
{
public Body body;
public int num;
public ContactArguments(Body body, int num)
{
this.body = body;
this.num = num;
}
};
public List<ContactArguments> callbacks = new List<ContactArguments>();
public List<SoundSource> sounds = new List<SoundSource>();
public AppWorldLogic()
{
}
public void contact_callback(Body body, int num)
{
callbacks.Add(new ContactArguments(body, num));
}
public void contact_callback_handler(Body body, int num)
{
if (num >= body.getNumContacts())
return;
// get coordinates of the contact point
Vec3 position = body.getContactPoint(num);
// get the relative impulse in the contact point
float impulse = body.getContactImpulse(num);
// calculate volume of the sound played for the contact
float volume = impulse * 0.5f - 1.0f;
// a flag indicating that no sounds have played yet
bool need_sound = true;
for (int i = 0; i < sounds.Count; i++)
{
if (sounds[i].isPlaying() == 0)
{
need_sound = false;
// reuse the existing sound source
sounds[i].setEnabled(1);
sounds[i].setGain(MathLib.saturate(volume));
sounds[i].setWorldTransform(MathLib.translate(position));
Sound.get().renderWorld(1);
sounds[i].play();
break;
}
}
if (need_sound)
{
// create a new sound source
SoundSource s = new SoundSource("static_impact_00.wav");
// specify necessary settings for the sound source
s.setOcclusion(0);
s.setMinDistance(10.0f);
s.setMaxDistance(100.0f);
s.setGain(MathLib.saturate(volume));
s.setWorldTransform(MathLib.translate(position));
// play the sound source
s.play();
// append the sound to the vector of the playing sounds
sounds.Add(s);
}
}
// method creating a box mesh with a physical body
public void create_box(Vec3 position)
{
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", new vec3(0.5f));
ObjectMeshStatic object_mesh = new ObjectMeshStatic(mesh);
object_mesh.release();
Editor.get().addNode(object_mesh.getNode());
object_mesh.setMaterial("mesh_base", "*");
object_mesh.setProperty("surface_base", "*");
object_mesh.setWorldTransform(MathLib.translate(position));
BodyRigid body = new BodyRigid(object_mesh.getObject());
new ShapeBox(body.getBody(), new vec3(0.5f));
// set a callback function that is run when a contact with the box occurs
body.setContactCallback(contact_callback);
}
public override int init()
{
// create box meshes
for (int i = -5; i <= 5; i++)
{
create_box(new Vec3(0.0f, i * 2.0f, 8.0f + i * 1.0f));
}
// create another box mesh
create_box(new Vec3(5.0f, 10.0f, 50.0f));
sounds.Clear();
callbacks.Clear();
return 1;
}
public override int update()
{
if (callbacks.Count > 0)
{
for (int i = 0; i < callbacks.Count; i++)
{
contact_callback_handler(callbacks[i].body, callbacks[i].num);
callbacks.Remove(callbacks[i]);
}
callbacks.Clear();
}
return 1;
}
public override int render()
{
return 1;
}
public override int flush()
{
return 1;
}
// end of the main loop
public override int shutdown()
{
foreach (var s in sounds)
s.clearPtr();
sounds.Clear();
return 1;
}
public override int destroy()
{
return 1;
}
public override int save(Stream stream)
{
return 1;
}
public override int restore(Stream stream)
{
return 1;
}
}
}
Last update: 2018-08-10
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)