sound panning 360 degrees in BGT working example and code

So, going to try this again. The previous code was rittled with mathematical errors, woops. Anyway, everything is fixed and I went ahead and commented out the function, if you don't understand sine cosine and arc_tangent, it won't help you much though, but it works now, I promise?

void thetaPos(double soundA, double soundB, sound@ SoundHandle, double myA, double myB, double myTheta)
{
//initialize our variables.
double absolutetheta;
double relativetheta;
double tempH;
double tempV;
double tVolume;
//find the lengths of the triangle we make to find the distance between our listener and the sound.
double temp1 = soundA-myA;
double temp2 = myB - soundB;

double d1=power(temp1, 2);
double d2=power(temp2,2);
//use distance formula
double d=square_root(d1+d2);
//if the distance we got is less than 75.
if(d<75)
{
//play our sound looped.
SoundHandle.play_looped();
//If our bottom leg, or the adjacent leg is to our right.
if(temp1>0)
{
//Find the longer leg and divide it by the shorter.
if(temp2>temp1)
{
absolutetheta=arc_tangent(temp2/temp1);
}
else
{
absolutetheta=arc_tangent(temp1/temp2);
}
}
//If our adjacent leg is to our left.
else if(temp1<0)
{
//Find the longer leg and divide it by the shorter.
if(temp2>temp1)
{
absolutetheta=arc_tangent(temp2/temp1)-90;
}
else
{
absolutetheta=arc_tangent(temp1/temp2)-90;
}
}
//if our adjacent leg is 0, I.E, our X coords are equal to our sound's X coords.
else if(temp1 == 0)
{
//Find the longer leg and divide it by the shorter.
  if (temp2 > 0)
{
absolutetheta=90;
}
else if(temp2<0)
{
absolutetheta=-90;
}
  else if (temp2 == 0)
{
absolutetheta=-1;
}
}
//Convert d egrees to Radians.
absolutetheta=absolutetheta*(180 /pi);
//Find our relative angle.
relativetheta = absolutetheta -myTheta-180;
//Cosine is adgacent
tempH=cosine((relativetheta*pi/180));
//Use the value from above to pan our sound.
SoundHandle.pan=tempH*100;
//Find our verticle measurement. Don't need to convert this one, <0 is in front, > is behind.
tempV=sine((relativetheta*pi/180));
tVolume=(d*-100)/75;
{

}
//if our sound is behind us.
if(tempV>0)
{
//Reduce the sound by a number between 0 and 10 based on how far to the left or right the sound is.
SoundHandle.volume=tVolume-(absolute(tempH)*1000/100);

//Find the absolute value of our horizontal value and use that to pitch bend our sound as it pans across the stereo field. As the sound moves away from 0 (centered). it will gradually decrease in pitch, as it approaches centered, it will raise in pitch.
SoundHandle.pitch= 80+absolute(tempH*5);
}
else
{
//Same as above, except raise the pitch as the sound is centered rather than decrease.
SoundHandle.volume=tVolume-(absolute(tempH)*1000/100);
SoundHandle.pitch=120-absolute(tempH*20);
}
}
else
{
//If the distance is greater than 75 pause the sound.
SoundHandle.pause();
}
}


Here's the example.

const double pi=3.141592653589793;
double x=0;
double degrees=270;
double y=0;
double soundx=10;
double soundy=10;
sound test;
void main()
{
show_game_window("test");
test.load("sounds/EnemyTeamMate.ogg");
test.play_looped();
do
{
if(key_pressed(KEY_S))
{
degrees-=180;
}
if(key_pressed(KEY_SPACE))
{
alert("HI", degrees);
}
if(key_down(KEY_RIGHT))
{
degrees+=.2;
}
if(key_down(KEY_LEFT))
{
degrees+=-.2;
}
if(degrees>359.95)
{
degrees=0;
}
if(degrees<0)
{
degrees=359.95;
}

thetaPos(soundx, soundy, test, x, y, degrees);

wait(5);

}
while(!key_pressed(KEY_ESCAPE));
}

Anyway, sorry for posting a dud earlier. This has been tested, please let me know if you find anything wrong with it.

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector

Reply via email to