Measuring the distance between two characters [SOLVED]

Started by Akatosh, Sat 20/01/2007 17:45:00

Previous topic - Next topic

Akatosh

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:

Code: ags
  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:

Code: ags

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?

Kweepa

#1
Your code will beep fastest if the tracer is on a diagonal with the player.
Here's what you want to do:

Code: ags


// 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);
}
Still waiting for Purity of the Surf II

Akatosh


Kweepa

Still waiting for Purity of the Surf II

Akatosh


SMF spam blocked by CleanTalk