Finding the distance

Blitz3D Forums/Blitz3D Programming/Finding the distance

PaulJG(Posted 2003) [#1]
I've got a X,Y,Z of my character - and a X,Y,Z of my object.

Now how can I find the approximate distance from one to the other ?

Thanks, Paul.


Ken Lynch(Posted 2003) [#2]
You can use EntityDistance(entity1, entity2)

or use the general equation

dx# = x0 - x1
dy# = y0 - y1
dz# = z0 - z1
dist# = sqr(dx# ^ 2 + dy# ^ 2 + dz# ^ 2)


Neochrome(Posted 2003) [#3]
which is faster?


Rob(Posted 2003) [#4]
Here's some tests...

Graphics3D 640,480,16,2

entA = CreatePivot()
PositionEntity entA,-100,124,0

entB = CreatePivot()
PositionEntity entB,0,24,500


;TEST blitz EntityDistance()
Print""
Time1#=MilliSecs()
For i=0 To 100000
	dist# = EntityDistance(entA,entB)
Next
Time2#=MilliSecs()
Print "EntityDistance took: "+(time2-time1) ;print the result
Print dist

;TEST custom DistA() routine
Print""
Time1#=MilliSecs()
For i=0 To 100000
	dist# = DistA(entA,entB)
Next
Time2#=MilliSecs()
Print "DistA took: "+(time2-time1) ;print the result
Print dist

;TEST custom DistB() routine
Print""
Time1#=MilliSecs()
For i=0 To 100000
	dist# = EntityDistance(entA,entB)
Next
Time2#=MilliSecs()
Print "DistB took: "+(time2-time1) ;print the result
Print dist

WaitKey
End


Function DistA#(a,b)
	dx# = EntityX(a) - EntityX(b)
	dy# = EntityY(a) - EntityY(b) 
	dz# = EntityZ(a) - EntityZ(b)
	Return Sqr(dx# ^ 2 + dy# ^ 2 + dz# ^ 2) 
End Function
 
Function DistB#(a,b)
	dx# = EntityX(a) - EntityX(b)
	dy# = EntityY(a) - EntityY(b) 
	dz# = EntityZ(a) - EntityZ(b)
	Return Sqr(dx#*dx# + dy#*dy# + dz#*dz#) 
End Function


DistB is the winner... slightly faster or the same as Blitz's own EntityDistance. It proves that power ^ is a very costly operation to use (see ken's code).


poopla(Posted 2003) [#5]
Their both the same thing really, the equation however uses points in space rather then entities as arguments, at it's simplest form.


Rob(Posted 2003) [#6]
As dev says, you can actually use points in space, making distb a useful function in its own right.


Ken Lynch(Posted 2003) [#7]
EntityDistance is quicker. I did a quick test and I can do a loop of 50,000 EntityDistance checks in about 2 millisecs compared to 45 millisecs for the other method.

Edit: But replacing dx#^2 with dx#*dx# gives much closer results, I had no idea that ^2 slowed things down that much.


Difference(Posted 2003) [#8]
Be sure to use the Global flag:

EntityX(a,True) etc.


Shambler(Posted 2003) [#9]
A^2 being slower than A*A is an eyeopener =) I wonder if ^2 is calling a floating point function...thanks for the info.


Ken Lynch(Posted 2003) [#10]
[quote]I wonder if ^2 is calling a floating point function.[quote]

dx# * dx# is also floating point. I think it must be slow because ^ uses a generic ^n function, so as long as you know you are squaring then use x*x, otherwise you will need to use x^n.


koekjesbaby(Posted 2003) [#11]
i don't know if it is important but you can skip the processor intensive sqr if you square all your distances (in advance, not in your main loop).

so instead of checking for a distance of 10 check for 100.

but, sometimes you need the actual distance so this does not apply...