This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
UnigineEditor
界面概述
资产工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
Setting Up Properties
照明
Landscape Tool
Sandworm (Experimental)
使用编辑器工具执行特定任务
Extending Editor Functionality
嵌入式节点类型
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

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.
Notice
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:

Source code (C#)
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.NumContacts)
                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)
                {
                    need_sound = false;

                    // reuse the existing sound source
                    sounds[i].Enabled = true;
                    sounds[i].Gain = MathLib.Saturate(volume);
                    sounds[i].WorldTransform = MathLib.Translate(position);
                    Sound.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.Occlusion = 0;
                s.MinDistance = 10.0f;
                s.MaxDistance = 100.0f;
                s.Gain = MathLib.Saturate(volume);
                s.WorldTransform = 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.SetMaterial("mesh_base", "*");
            object_mesh.WorldTransform = MathLib.Translate(position);
			object.SetCollision(1, 0);

            BodyRigid body = new BodyRigid(object_mesh);
            new ShapeBox(body, new vec3(0.5f));
            // set a callback function that is run when a contact with the box occurs
            body.AddContactCallback(contact_callback);

        }

        public override bool 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 true;
        }

        public override bool 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 true;
        }

        public override bool PostUpdate()
        {

            return true;
        }

        public override bool UpdatePhysics()
        {

            return true;
        }
        // end of the main loop

        public override bool Shutdown()
        {

            foreach (var s in sounds)
                s.clearPtr();
            sounds.Clear();


            return true;
        }

        public override bool Save(Stream stream)
        {

            return true;
        }

        public override bool Restore(Stream stream)
        {

            return true;
        }
    }
}
Last update: 2020-11-24
Build: ()