• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

[solved] Need help with audio_play_at in 3D game

M

MHBlacky

Guest
Hey guys,

I am currently working on a first person shooter and until now I always only used audio_play_sound for all sounds.
Today I wanted to start setting up proper 3D sounds for the enemies' guns. So that I get a falloff depending on the distance to the enemy and also a pan change depending on where I look.

What I did: I the player's step event I update the listener's position:
Code:
// Position
audio_listener_position(x, y, z);

// Orientation
var r = 1; // radius (doesn't matter here because it's only about the orientation not the vector length)
var lookat_x = x + r * sin( degtorad(90+obj_player.pitch) ) * cos( degtorad(-obj_player.viewdirection) );
var lookat_y = y + r * sin( degtorad(90+obj_player.pitch) ) * sin( degtorad(-obj_player.viewdirection) );
var lookat_z = z + r * cos( degtorad(90+obj_player.pitch) );
var up_x = 0;
var up_y = 0;
var up_z = 1;
audio_listener_orientation(lookat_x, lookat_y, lookat_z, up_x, up_y, up_z);
lookat_x/y/z should definitely work because the code is the same that I also use as my camera's look-at-point.
As up_x/y/z I just used the z-axis because the player's 3D body orientation never changes so the up direction is my game is always along the z-axis.

Then in my enemy object I use the following code when shooting:

Code:
audio_play_sound_at(snd_rifle, x, y, z, 100, 50, 1, false, 1);
The sound falloff works perfectly fine but the problem is that the pan seems to ignore the 3rd dimension completely.
When the player's x-value is smaller than the enemy's the sound is played more on the right speaker and the other way round when the player's x value is larger. The listener's orientation seems to be ignored completely...
Have tried the same thing with an emitter --> same problem.

Am I missing something obvious here? Does anyone maybe have an idea how i can get the pan to work properly?
 
I don't think you should be adding x,y,z to your lookat vector. I'm going to double check that in a moment. EDIT: yes, I think I am correct about this.

Also, make sure you sounds are checked "3d", if you haven't done so already.

Why are you adding 90 to pitch?

P.s., sin and cos have counterparts that take angle argument in degrees: dcos, dsin.
 
M

MHBlacky

Guest
Yes, I've noticed that mistake in the meantime. All 3D sounds are also checked as 3D. I'm adding 90 to my pitch because the way I calculate it it's the angle from the z axis down to the x-y-plane and not the other way round. To compensate that I add 90. That's fine. If I change that everything goes completely in the wrong direction xD
The problem now is that the sound ALWAYS plays on the wrong side when I rotate my camera around. So I tried to invert my viewdirection to solve that but then this happened:

Unbenannt.png

Now when my x is higher / lower than the enemy's everything works fine but if my y is smaller higher / lower than his' it's still the wrong direction. I don't get it xD
 
M

MHBlacky

Guest
Oh wow... I got it... my up_z needs to be -1 instead of 1. Dunno why because usually positive z should mean up but whatever. Now it works :D Thanks for your help ^-^ and also thanks for the advice with sind and cosd. Really didn't know they exist xD
 
@MHBlacky for some reason, in GMS2 the up vector is upside down. I've never seen a convincing explanation for why this should be. In GMS1.4, the up vector is correctly in the direction you specify (not upside down).
 
Top