Posted: 30th Jan 2003 18:38
I have found out, that function Length Vector3 (Vector2), is more faster than standard function of distance between two objects Sqrt ((SourcePointestPointX) ^2 + ((SourcePointY-DestPointY) ^2 + ((SourcePointZ-DestPointZ) ^2). The following code shows it.

+ Code Snippet
r=Make Vector3(1)
Make Object Cube 1, 1
Position Object 1, Rnd(20), Rnd(20), Rnd(20)

Make Object Cube 2, 1
Position Object 2, Rnd(20), Rnd(20), Rnd(20)

Sync Rate 0
Sync On

Position Camera 0, 10, -50

x1#=Object Position X(1)
y1#=Object Position Y(1)
z1#=Object Position Z(1)

x2#=Object Position X(2)
y2#=Object Position Y(2)
z2#=Object Position Z(1)

Do

   Timer1=Timer()
   For i=1 To 100000
      dist1#=Sqrt((x1#-x2#)*(x1#-x2#)+(y1#-y2#)*(y1#-y2#)+(z1#-z2#)*(z1#-z2#))
      `dist1#=Sqrt((x1#-x2#)^2+(y1#-y2#)^2+(z1#-z2#)^2)
   Next i
   Timer1=Timer()-Timer1

   Timer2=Timer()
   For i=1 To 100000
      Set Vector3 1,(x1#-x2#),(y1#-y2#),(z1#-z2#)
      dist2#=Length Vector3(1)
   Next i
   Timer2=Timer()-Timer2

   Text 10, 10, "FPS = "+Str$(Screen FPS())
   Text 10, 30, "Distance (Sqrt()) = "+Str$(dist1#)+" Time = "+Str$(Timer1)
   Text 10, 50, "Distance (Length Vector3)) = "+Str$(dist2#)+" Time = "+Str$(Timer2)
   Sync
Loop
Posted: 30th Jan 2003 19:58
Dmitros, does the 2nd dist1# calculation run slower than the other dist1# on your computer as well? Btw nice tip.
Posted: 30th Jan 2003 20:23
Yes it's so.
1st dist1# approx. 24
2nd dist1# approx. 150
dist2# approx. 17
Posted: 30th Jan 2003 20:24
its because its a single sum for all three points at once rather than 3 sums in one
but the speed increase is probably minimal
Posted: 1st Feb 2003 16:15
To make this a fair test, you need to alter the dist1# test slightly.

The Line: dist1#=Sqrt((x1#-x2#)*(x1#-x2#)+(y1#-y2#)*(y1#-y2#)+(z1#-z2#)*(z1#-z2#)), has things like x1#-x2# calculated twice, when idealy you only need to do this once.

xd# = x2#-x1#
yd# = y2#-y1#
zd# = z2#-z1#
dist1# = Sqrt(xd#*xd# + yd#*yd# + zd#*zd#)

On my system, I save about 4 milliseconds, oooh! hold the front page!

Though this makes dist1# a bit faster than before it's still fractionly slower than using vector3 (about 4-5 milliseconds slower for me).