Play_3dsound

Posted By: _Tommo_

Play_3dsound - 10/28/05 20:51

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
Posted By: Lion_Ts

Re: Play_3dsound - 10/28/05 22:31

O! I have to try...
thank you
Posted By: Inestical

Re: Play_3dsound - 10/31/05 22:23

on ride tommorow! Going to test that one
Posted By: _Tommo_

Re: Play_3dsound - 11/01/05 21:51

Hey i found some little errors... replace the volume part of the function with this:
Code:
 
volume = radius / 10;
volume -= vec_dist(camera.x,vec_pos) * volume / max(0.1,radius); //result:radius = x:volume -- ex with result = 10: 10:700=x:70

trace_mode = ignore_models + ignore_passents + ignore_passable;
if(trace(vec_pos,camera.x) != 0) {volume /= 2;}



And tell me if it works for you
Posted By: BionicHero

Re: Play_3dsound - 11/03/05 13:04

Hi! I didn't test the script and I don't want to ruin for anyone who wants to use it but this is actually no 3d sound. It's just a stereo sound which means, you would never be able to hear it BEHIND you, right?
Posted By: _Tommo_

Re: Play_3dsound - 11/03/05 21:07

Yeah, actually it works for stereo speakers, but because i don't have Surrond to test (I tested it with a headset) i can't say if it will work on it... if you hear the sound from behind, it will just play with a balance of 0. I made it just to use it insistead of ent_playsound, and sorry if i gave to it a wrong name...
Posted By: snake67

Re: Play_3dsound - 11/20/05 15:01

Thanks a lot for this contribution! Its exactly what i needed. To play a hit sound on level geometry i used a sprite for the hit flash so i could do it with ent_playsound. With your code i can do the effect with a particle wich is faster.
© 2024 lite-C Forums