Hi,
for my first contribute... here it is this simple but useful (at least for me
) function that allows to play a 3d sound at an arbitrary position. I made this becaus ent_playsound is buggy and needs an entity as emitter. My function needs the sound name string, the position vector of the sound source and the radius (from 0 to unlimited) of the sound. The obstacle detection is very simple for speed reason, and it's better to use short sounds because they aren't dynamically updated.
Code:
function play_3dsound(str_snd,&vec_pos,radius)
{
var snd_handle;
var volume;
var balance;
var balance_angle[3];
//--volume calculations
volume = radius / 10;
result = vec_dist(camera.x,vec_pos);
volume = volume - int(result * volume / radius); // result:radius = x:volume -- es with result = 10: 10:700=x:70
volume = int(clamp(volume,1,100));
if(trace(vec_pos,camera.x) != 0) {volume /= 2;} //cut this if you dont want object detection
//----balance calculations
//balance for pan
vec_sub(vec_pos,camera.x);
balance_angle.pan = atan(vec_pos[1]/vec_pos[0]); //find the angle from camera.x to the emitter
balance_angle.pan = ang(camera.pan) - balance_angle.pan; //find the difference between cam.pan and bal.pan
temp = abs(balance_angle.pan);
temp = (temp - (temp - 90)*2*(temp > 90)) / 90 * 50; //temp is locked to 0-90, 0 = 0 and 90 = 100, if temp is bigger, its inverted.
balance = temp * sign(balance_angle.pan); //find if on the right or left headphone.
//balance for roll
balance_angle.roll = atan(vec_pos[2]/vec_pos[1]); //find the angle from camera.x to the emitter
balance_angle.roll = ang(camera.roll) - balance_angle.roll; //find the difference between cam.pan and bal.pan
temp = abs(balance_angle.roll);
temp = (temp - (temp - 90)*2*(temp > 90)) / 90 * 50; //temp is locked to 0-90, 0 = 0 and 90 = 100, if temp is bigger, its inverted.
balance += temp * sign(balance_angle.roll); //find if on the right or left headphone.
//--------
return(snd_play(str_snd,volume,balance));
}
I hope that you'll find it useful... and sorry for the bad english