AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   wierd results from get_distance (https://forums.alliedmods.net/showthread.php?t=51539)

genesis 02-19-2007 17:37

wierd results from get_distance
 
1 Attachment(s)
./,),(-*,(.((((((

get_distance and get_distance_f seem to be returning wierd results to me, at first I thought it was isolated but i upgraded the amxx version to 1.76c and I still have the problem. does anyone know why it would return values like ./,),(-*,(.(((((( i was displaying this as a float.

on a side topic, i built my own distance function and while it didnt return values like the one above it returns either very large numbers or negative numbers and just gives unpredictable results does anyone know why it does?

attached is the custom distance function

if i recall the distance forumula is somewhat simple:
sqroot((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)

XxAvalanchexX 02-19-2007 19:15

Re: wierd results from get_distance
 
If you can sucessfully reproduce it, like you say, then I'd submit a bug report.

[ --<-@ ] Black Rose 02-20-2007 01:21

Re: wierd results from get_distance
 
sqroot( ( x1 - x2 )^2 + sqroot( ( y1 - y2 )^2 + ( z1 - z2 )^2 )^2 )

This only works if: x1 >= x2 && y1 >= y2 && z1 >= z2

I believe this would be the correct useage, made this just to test some weeks ago.

Code:
stock cust_get_distance(const Origin1[3], const Origin2[3]) {         new Differences[3]         if ( Origin2[0] > Origin1[0] )         Differences[0] = Origin2[0] - Origin1[0]     else         Differences[0] = Origin1[0] - Origin2[0]         if ( Origin2[1] > Origin1[1] )         Differences[1] = Origin2[1] - Origin1[1]     else         Differences[1] = Origin1[1] - Origin2[1]         if ( Origin2[2] > Origin1[2] )         Differences[2] = Origin2[2] - Origin1[2]     else         Differences[2] = Origin1[2] - Origin2[2]         return sqroot(power_by_2(Differences[2]) + power_by_2(sqroot(power_by_2(Differences[0]) + power_by_2(Differences[1])))) } stock Float:cust_get_distance_f(const Float:Origin1[3], const Float:Origin2[3]) {         new Float:Differences[3]         if ( Origin2[0] > Origin1[0] )         Differences[0] = Origin2[0] - Origin1[0]     else         Differences[0] = Origin1[0] - Origin2[0]         if ( Origin2[1] > Origin1[1] )         Differences[1] = Origin2[1] - Origin1[1]     else         Differences[1] = Origin1[1] - Origin2[1]         if ( Origin2[2] > Origin1[2] )         Differences[2] = Origin2[2] - Origin1[2]     else         Differences[2] = Origin1[2] - Origin2[2]         return floatsqroot(power_by_2_f(Differences[2], 2) + power_by_2_f(floatsqroot(power_by_2_f(Differences[0]) + power_by_2_f(Differences[1])))) } stock Float:power_by_2_f(Float:num)     return num * num     stock power_by_2(num)     return num * num

XxAvalanchexX 02-20-2007 01:48

Re: wierd results from get_distance
 
Instead of all those ifs I'd just use abs/floatabs.

[ --<-@ ] Black Rose 02-20-2007 10:24

Re: wierd results from get_distance
 
Quote:

Originally Posted by XxAvalanchexX (Post 442791)
Instead of all those ifs I'd just use abs/floatabs.

I don't know what abs is... ^^

stupok 02-20-2007 17:33

Re: wierd results from get_distance
 
Why would you do that, Black Rose? btw, abs is absolute value, for example abs(-1) = 1

Code:
#include <amxmodx> public plugin_init() {     new integer_value = 1 - 2     new Float:float_value = 1.0 - 2.0     server_print("POWER: %i, %f", (integer_value * integer_value), (float_value * float_value))     server_print("DISTANCE: %i, %f", sqroot(integer_value * integer_value), floatsqroot(float_value * float_value)) }

Code:

POWER: 1, 1.000000
DISTANCE: 1, 1.000000

I'd wager that genesis is putting a %s where a %f should be or something along those lines. I doubt there is anything wrong with the distance functions.

genesis 02-20-2007 23:28

Re: wierd results from get_distance
 
i dont think it should matter (mathematically) whether x1 >= x2 because lets say it isn't

Assume x1 = -5 and x2 = 2, adding them together will give -3, well distances can't be negative right, but then you square that, and anything squared is positive so it shouldn't matter, however, somewhere in the programming it may get messed up, I ended up ripping out a function out of a plugin on the forums the one that lets you voice chat to only nearby players... this method DOES work and works well...

Code:
stock Float:vecdist(Float:vec1[3], Float:vec2[3]) {         new Float:x = vec1[0] - vec2[0]         new Float:y = vec1[1] - vec2[1]         new Float:z = vec1[2] - vec2[2]         x*=x;         y*=y;         z*=z;         return floatsqroot(x+y+z); }

[ --<-@ ] Black Rose 02-21-2007 00:56

Re: wierd results from get_distance
 
Quote:

Originally Posted by genesis (Post 443176)
i dont think it should matter (mathematically) whether x1 >= x2 because lets say it isn't

Assume x1 = -5 and x2 = 2, adding them together will give -3, well distances can't be negative right, but then you square that, and anything squared is positive so it shouldn't matter, however, somewhere in the programming it may get messed up, I ended up ripping out a function out of a plugin on the forums the one that lets you voice chat to only nearby players... this method DOES work and works well...

Code:
stock Float:vecdist(Float:vec1[3], Float:vec2[3]) {         new Float:x = vec1[0] - vec2[0]         new Float:y = vec1[1] - vec2[1]         new Float:z = vec1[2] - vec2[2]         x*=x;         y*=y;         z*=z;         return floatsqroot(x+y+z); }

-5 - 3 = -7, subtraction not addition.

No it doesn't

Code:
stock Float:vecdist(Float:vec1[3], Float:vec2[3]) {         new Float:x = vec1[0] - vec2[0]         new Float:y = vec1[1] - vec2[1]         new Float:z = vec1[2] - vec2[2]         x*=x;         y*=y;         z*=z;         return floatsqroot( x + floatsqroot(y+z) * floatsqroot(y+z) ); }
Code:
stock _get_distance(const Origin1[3], const Origin2[3]) {         new Diff[3]         Diff[0] = Origin1[0] - Origin2[0]     Diff[1] = Origin1[1] - Origin2[1]     Diff[2] = Origin1[2] - Origin2[2]         Diff[0] *= Diff[0]     Diff[1] *= Diff[1]     Diff[2] *= Diff[2]     new Temp = sqroot(Diff[0] + Diff[1])         return sqroot(Diff[2] + Temp * Temp) } stock Float:_get_distance_f(const Float:Origin1[3], const Float:Origin2[3]) {         new Float:Diff[3]         Diff[0] = Origin1[0] - Origin2[0]     Diff[1] = Origin1[1] - Origin2[1]     Diff[2] = Origin1[2] - Origin2[2]         Diff[0] *= Diff[0]     Diff[1] *= Diff[1]     Diff[2] *= Diff[2]     new Float:Temp = floatsqroot(Diff[0] + Diff[1])         return floatsqroot(Diff[2] + Temp * Temp) }
Seems to work.

VEN 02-21-2007 08:05

Re: wierd results from get_distance
 
Author, can you show us how exactly to reproduce the issue?

genesis 02-25-2007 15:42

Re: wierd results from get_distance
 
1 Attachment(s)
Sure, here is a simple plugin that gives me the problem

to test go load plugin go into game and say /testdistance and it will tell you your distance to the arbitrary point (150, 150, 150)

which comes out gibberish.


All times are GMT -4. The time now is 00:35.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.