Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akatosh on Sat 20/01/2007 17:45:00

Title: Measuring the distance between two characters [SOLVED]
Post by: Akatosh on Sat 20/01/2007 17:45:00
Descriptive Subject, huh? Okay, the problem is: In _Access, you will sometimes be hunted by tracers. I want the game to play a "beep" sound every x game cycles, where x depends on the distance between the player and the tracer. I use this code to determine the distance:

  int distancey=player.y-cTrac.y;
float dy=IntToFloat(distancey*distancey);
distancey=FloatToInt(Maths.Sqrt(dy));
 
int distancex=player.x-cTrac.x;
float dx=IntToFloat(distancex*distancex);
distancex=FloatToInt(Maths.Sqrt(dx));

int distance=distancex-distancey;
float d=IntToFloat(distance*distance);
distance=FloatToInt(Maths.Sqrt(d));


The sqrt thing is to ensure the result is always positive. Now, the code for playing the sound:


if (IsTimerExpired(1)){
PlaySound(0);
//determine distance
SetTimer(1, distance/2);


But this code somehow screws up pretty fast. It works, yes, but not perfect. Any suggestions how to make this a little more error-resistant?
Title: Re: Measuring the distance between two characters
Post by: Kweepa on Sat 20/01/2007 17:56:23
Your code will beep fastest if the tracer is on a diagonal with the player.
Here's what you want to do:



// startup
SetTimer(1, Random(100));

// repex
if (IsTimerExpired(1)) {
  PlaySound(0);
  int minTime = 8;
  float dx = IntToFloat(player.x - cTrac.x);
  float dy = IntToFloat(player.y - cTrac.y);  // FIXED player.x -> player.y
  int distance = FloatToInt(Maths.Sqrt(dx*dx + dy*dy));
  SetTimer(1, minTime + distance/2);
}
Title: Re: Measuring the distance between two characters
Post by: Akatosh on Sat 20/01/2007 18:11:21
Does not work as intended, sorry.  :P
Title: Re: Measuring the distance between two characters
Post by: Kweepa on Sat 20/01/2007 18:16:14
Fixed. My apologies.
Title: Re: Measuring the distance between two characters
Post by: Akatosh on Sat 20/01/2007 18:46:02
Works now. Thank you!