Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: cornjob on Fri 15/08/2003 09:12:22

Title: square root & math
Post by: cornjob on Fri 15/08/2003 09:12:22
So anyway, I'm trying to calculate the distance between 2 points on the screen, to see if a point is within a certain radius of another point... and...

Well, I was never really a math person. The best idea I could come up with is using the pythagorean theorem... But is there a square root operation in AGS? If not, how can I make one? Can I somehow use the functions in Erica's math plugin? (Sorry, like I said, my math is very limited.) Or is there a better way to do this? I'm guessing there is.

OK, thanks. (my first tech question in years)
Title: Re:square root & math
Post by: Gilbert on Fri 15/08/2003 09:19:10
No, and since AGS doesn't support float data type, you won't get much of it, you may try Erica's math plugin though, that there may be someway round it.

If you just want to have just a approximate integer portion of sqrt, there is a foolish (but wiorking) solution:

function sqrt(int inputnum){
int i = 0;
while ((i*i)<inputnum) {i++;}
if ((i*i)==inputnum){return i;}
else {return (i - 1);}
}

You may tweak it so it return the closest integer, etc.
Title: Re:square root & math
Post by: Fuzzpilz on Fri 15/08/2003 09:19:32
You don't need the root, just use the square of the radius.
Title: Re:square root & math
Post by: Gilbert on Fri 15/08/2003 09:21:32
Yep in your case fuzz's solution should work too.
Title: Re:square root & math
Post by: cornjob on Fri 15/08/2003 12:09:44
OK, thanks guys... I'll try this out.
Title: Re:square root & math
Post by: Ghormak on Fri 15/08/2003 12:38:44
True what Fuzz says. Say, if you want to see if (x1,y1) and (x2,y2) are within 8 pixels from each other, you'd simply check if ( (x2 - x1)^2 + (y2-y1)^2 ) is less than 64 (8^2). No need for the square root if you don't need to actually display the distance. I'm sure you knew this already, but I thought I'd clear it up if anyone else was wondering.

(correct me if I made any mistakes, my math is a bit rusty nowadays)
Title: Re:square root & math
Post by: Gilbert on Mon 18/08/2003 03:43:41
Heh all right, but Ghor, better post the codes as:
... check if ( (x2 - x1) * (x2 - x1) + (y2-y1) * (y2-y1) ) is less than ...

Or people who actually tried it by copying the codes will get an error, as there's no raise to power operator in AGS ;)
Title: Re:square root & math
Post by: Ghormak on Mon 18/08/2003 22:48:38
Ah, that's what I get for not working with AGS for months. Thanks Gil. :)

/me gets to work on Moose Wars 2... maybe