Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Distance between two objects

Author
Message
Dmitry K
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Russia
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.

haggisman
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
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.

Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp
Dmitry K
21
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Russia
Posted: 30th Jan 2003 20:23
Yes it's so.
1st dist1# approx. 24
2nd dist1# approx. 150
dist2# approx. 17

Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
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

Anata aru kowagaru no watashi!
Easily Confused
21
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
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).

Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.

Login to post a reply

Server time is: 2024-04-20 05:10:11
Your offset time is: 2024-04-20 05:10:11