Forum
CS2D Scripts Distance between playersDistance between players
13 replies 1
So basically you can do it like this:
1
distance = math.sqrt(player(id1,"x") - player(id2,"x") + player(id1,"y") - player(id2,"y"))
You didn't "square" the variables:
1
distance = math.sqrt((player(id1,"x") - player(id2,"x"))^2 + (player(id1,"y") - player(id2,"y"))^2)
1
2
3
4
2
3
4
side1 = math.max(player(id1,"x"),player(id2,"x"))-math.min(player(id1,"x"),player(id2,"x")) side2 = math.max(player(id1,"y"),player(id2,"y"))-math.min(player(id1,"y"),player(id2,"y")) distance = math.sqrt(side1^2+side2^2)
You cant use math.sqrt on a negative number
I don't know, but my version worked normal for me.
player 1 X100 Y100
player 2 X200 Y300
sqrt(100 - 200 ^ 2 + 100 - 300 ^ 2)
sqrt(100 - 40000 + 100 - 90000)
sqrt(-39900 - 89900)
sqrt(-129800)
no number multiplyed by himself can be a negative number
example
2^2 = 2*2 = 4
-2^2 = -2*-2 = 4
Unknown_Soldier has written
@Vectar666 No, I didn't see them. Sry , I have problems with brackets when I make luas. Anyway, both codes should work.
and, I'd prefer math.abs instead of math.max with math.min.
Just a little less space eaten by it.
1
2
3
4
5
6
2
3
4
5
6
function math.dist(p1, p2) 	local x, y = player(p1, "x"), player(p2, "y"); 	local x2, y2 = player(p2, "x"), player(p2, "y"); 	 	return math.sqrt((y2-y)^2 + (x2-x)^2); end
Just call it with two player IDs.
Ex.:
1
dist = math.dist(1, 32);
Vectar666 has written
You didn't "square" the variables
Fudge cakes. It was late and I was tired, what can I say?
1