Audio.Apply3D() for 2D

Hello everybody! Please help me, how do I need to set the Forward and Up properties of the AudioListener and AudioEmmiter objects correctly so that it works in 2D?

Not sure if Audio makes some distinct differences, but have you tried Vector3.Up and Vector3.Forward?

The problem is that I can’t figure out how to specify these vectors for 2D. Imagine that I have a broadcasting circle and there is a listener. In this case, how do I specify the values of the Forward and Up vectors?

I would expect the AudioListener needs a Forward + Upward Vector to correctly determine where left/right is to for stereo etc.

That said - you’d specify those vectors in respect to whereever your “head” should be … in most 3D cases this will be your camera - in 2D it may or may not be the player and you’d just take whatever vectors that listeners is going to have.

But even in 2D I would expect the listener to be “me” (the camera) - and in that case you specify Vector3.Forward + Vector3.Up.

There is no such thing as “2D” … what we render as 2D nowadays are still just quads in a 3D world (with z = 0) and an orthogonal projection.

So Vector3.Forward is the Vector you (the viewer has - which may differ between opengl and dx, but the Vector3.Forward should care for it) and your Up-Vector should technically be Vector3.Up + whatever rotation you have on your View (if any)

could be you can get those vectors from the projection matrix as well, but I am not sure

1 Like

Don’t think of the AudioListener as positioning the emitter relative to the character in your game world: think of it as positioning the emitter relative to the player sitting in front of a screen with headphones on. Forward is the axis “into” the screen, the one you’re not using in your 2d game (usually the Z axis).

In that space there’s no concept of “2d audio”. If you say you want a sound to come from the left, do you mean the left-hand side of the screen in front of them, or all the way over 90 degrees as if it’s being emitted directly into their left ear?

Intuitively I’d probably try out the first one first, since it’s an easier translation: if you place the AudioListener at (0,0,0) and the Audio Emitter at (x,y,1) where x ranges from -1 to 1 at the borders of the screen.

But the most important thing is to remember that you’re positioning the sound not in the game-space of the player character, but in the real-world space of the person playing your game.

1 Like

Thanks for the good explanation! Is it possible to assume that if the Emitter is in the center of the screen, and the Listener goes outside the window, then the sound will not be heard? I’m wondering at what units of sound is not heard.

I had to make my idea through an instance of sound. Thank you all for participating. I’ll leave the code here, maybe it will be useful to someone.

void ISoundEmitter.ApplyDistancing()
        {
            float distance = Vector2.Distance(GameObject.World.Position, listener.GameObject.World.Position);
            float pan = 0f;
            float volume = 1f;

            if (distance <= MaxRadius)
            {
                if (distance > MinRadius)
                {
                    float ratio = (distance / MaxRadius);

                    volume = 1 - ratio;

                    var differenceX = (GameObject.World.Position - listener.GameObject.World.Position).X;

                    if (differenceX != 0)
                    {
                        var differenceAbs = MathF.Abs(differenceX);

                        if (differenceAbs >= MinRadius / 2)
                            pan = ratio * (differenceX / differenceAbs);
                    }

                }
            }
            else volume = 0f;

            effectInstance.Volume = volume;
            effectInstance.Pan = pan;

            Debug.WriteLine($"Volume: {effectInstance.Volume}, Pan: {effectInstance.Pan}");
        }
1 Like