Top down 2D spatial sounds

Using my own engine and the origin 0,0 is at the top left with positive +Y means going down
image

My game is a top down 2D pixel art shooter like nuclear throne/enter the gungeon, but there can be a floor ontop of the player (I call that Z in my game)
I’m trying to make gunshot sounds have a spatial effect based on distance and floor they are on and I’m not sure if I’m setting up the vectors correctly:

I think what I want is a left handed coordinate system where Z is up and y is pointing down(the bottom left of the figure)? So that way I can just pass in my tile coordinates x: 5, y: 10, z: 2 (entity to the right going downwards but on 2nd floor)

// init
            FMOD_System_SetSoftwareFormat(core, 0, FMOD_SPEAKERMODE::FMOD_SPEAKERMODE_DEFAULT, 0);
            FMOD_Studio_System_Initialize(
                system,
                1024,
                FMOD_STUDIO_INIT_NORMAL,
                FMOD_INIT_NORMAL,
                std::ptr::null_mut(),
            );

let mut listener = FMOD_3D_ATTRIBUTES { 0 };
listener.forward.z = -1.0;
listener.up.y = -1.0;

fn play_gunshot_event() {
    // forward and up are the same
    let mut attr = listener; // make sure forward and up are the same as listener
    attr.pos.x = tile_x;
    attr.pos.y = tile_y;
    attr.pos.z = tile_z;

     FMOD_Studio_EventInstance_Start(instance);
     FMOD_Studio_EventInstance_Set3DAttributes(instance, &mut attr);
     FMOD_Studio_EventInstance_Release(instance);
}

fn update(lpos: Vector3) { //basically takes in the camera position
            listener./
            FMOD_Studio_System_SetListenerAttributes(
                self.studio_sys,
                0,
                &listener,
                &FMOD_VECTOR::default(),
            );
            FMOD_Studio_System_Update(self.studio_sys);

}

I end up hearing no sound. Setting the position of the listener seems to have very little effect(not none though) on how the sound is spatialized compared to setting the event instance 3d attributes. I played around with having a gui slider to modify these values. Am I not supposed to move the listener? Setting the listeners position to be x: 15, y: 15, z: 15, and the gunshot event position to same pos leads to no sound?! Why is that? I thought it would lead to max sound since listener and event are @ same location

Hi,

I will pass on these two forums that were experiencing similar issues:
Forums.
(3D Attributes in a 2D game - #3 by jmcmorris)
(2D positonal sounds)

Could you elaborate on:

as I am confused about the level setup.

You can move the listener, you can either attach it to the player character or the main camera. More information about the listener can be found here: FMOD Studio | Glossary - Listener.

Unfortunately, as it is a custom engine I am not sure why this is happening.
Would it be possible to enable live update and record a profiler session and keep an eye on the location of the events in the 3D view and make sure the positions of everything is as expected.


Let’s ignore Z for now and just think of 2D top down from top left.
Sounds should be playing when the fired bullet hits the glass, and as you can see in the 3D panner, the positions are set correctly but no sound is being outputted(I am recording sound in this clip). I set it to orhto and Z is not moving up or down either? So I’m guessing its not an issue with Z?

Below is a terminal printing out the position, forward, and up vectors that are fed to FMOD_Studio_System_SetListenerAttributes and FMOD_Studio_EventInstance_Set3DAttributes. These are in tile coordinates (0,0) being top left.

The code is the same in my original post.

play_gunshot_event(gun_shot_pos); // creates oneshot event instance, sets 3d attributes and releases with same up and forward as listener using: FMOD_Studio_EventInstance_Set3DAttributes
update_listener_pos(camera_pos); //FMOD_Studio_System_SetListenerAttributes the camera tile position
FMOD_Studio_System_Update();

The radius is fmods default of 20 and as you can see the positions that are fed in are not exceeding that limit either.
I’m very confused as to why sound isn’t playing still.
What am I doing wrong?
This works fine if I don’t move the listener(keep it 0,0) and then map all sound coords relative to this origin, e.g (x: 3, y: 5) would mean a sound is coming from bottom right, but once I set listener&event position to use my world space coords, it doesne’t make a sound :confused:

            FMOD_Studio_System_SetListenerAttributes(
                self.studio_sys,
                0,
                &listener,
                &FMOD_VECTOR::default(),
            );

The problem was right infront of me the whole time :rofl:.

Passing NULL for attenuationposition will result in the listener only using the position in attributes.

I did not pass NULL… I passed &FMOD_VECTOR::default() which is a stack pointer to a zeroed out vector. It works now, thanks guys

https://www.fmod.com/docs/2.02/api/studio-api-system.html#studio_system_setlistenerattributes
When would one want to have the attenuation position not be the same as the listener position?

Thank you for sharing your solution.

An attenuation object would be useful when spatialization and panning need to be disconnected. E.g. in your game, you would want spatialization based on your player character while panning based on the camera’s perspective.

Hope this helps