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.

Newcomers DBPro Corner / distances in 3d

Author
Message
DBasic Khan
20
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: Alpha Quadrent, Sector 1,1 SOL SYS
Posted: 9th Mar 2004 20:33
does anyone know the mathamatical formula to calculate distance between 2 points in 3D space? I know the one for 2D:
squar root((x1-x2)+ (y1-y2))
but what is it for 3D?

Im using Dbasic Classic
ReD_eYe
21
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 9th Mar 2004 20:40


thats the function i use, seems to work

In the beginning, the universe was created...
This made alot of people very angry and it has been widely regarded as a bad idea...
Visit http://redeye.dbspot.com
DBasic Khan
20
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: Alpha Quadrent, Sector 1,1 SOL SYS
Posted: 9th Mar 2004 21:04
yes but that should only work in 2d
ReD_eYe
21
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 9th Mar 2004 21:06
except it works in 3d, trust me, i've used it many times...

In the beginning, the universe was created...
This made alot of people very angry and it has been widely regarded as a bad idea...
Visit http://redeye.dbspot.com
Tim Ballisto
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Right behind you.
Posted: 10th Mar 2004 02:53 Edited at: 10th Mar 2004 02:57
i do believe this is the code you are looking for.


Distance = sqrt((object position x(object1)-object position x(object2))^2 + (object position y(object1)-object position y(object2))^2 + (object position z(object1)-object position z(object2))^2)

i learned this code from hyrichter(i think), so if it's wrong blame HIM, and if it's right thank ME!
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 10th Mar 2004 14:56
Yuck, thats a lot of typing. Why not just use my function? Its much more user friendly. I put it into the codebase here: http://darkbasicpro.thegamecreators.com/?m=codebase_edit&i=59748ee96250002a4455566559c1856a

Cheers

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 10th Mar 2004 14:57
... and if that link doesn't work, try this code box:



Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home
hyrichter
20
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 10th Mar 2004 16:38
Yes, Tim, you did get that code from me. But I learned it from the Monster Hunt tutorial, so blame THEM if it doesn't work!!
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 10th Mar 2004 20:43


lol, this looks remarkably similar to a function I wrote and circulated a few years ago.



I was once supplied with my own function when I asked a question - how kind

"To do is to be" - Descartes
"To be is to do" - Voltaire
"Do be do be do" - Frank Sinatra
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 13th Mar 2004 08:16 Edited at: 13th Mar 2004 08:17
As you'd expect, using the SQRT command (per David T's snippet) is fractionally faster than setting up a DBPro Vector3 and applying the length vector command to it (per my code). This is because the length vector command will itself perform a square root calculation. Both methods ultimately have to use pythagorus' equation.

See:



My computer's results for this are:

SQRT route: 2671
Length vector route: 2750

However, the length vector command option will be very considerably faster when you already have vectors created. This is because you no longer have the overhead of making a vector3 each time.

See, for example:



In this situation my results are:

SQRT route: 3511
Length vector route: 120

So thats quite a dramatic difference.

The one thing that does puzzle me with this is why the SQRT route seems to take longer when the function call is omitted. I would have thought the converse should be true.

Cheers

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 13th Mar 2004 08:37
Theres one other point I should make. It is that if you are going to use the SQRT method then the ^ 2 mathematical calculation should be avoided if possible. In computational terms this is a slow calculation. Its better to use multiplication instead.

See:



My computer gives the following results:

^ 2 method: 3538
Multiplication method: 567

So, again, a dramatic difference.

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 13th Mar 2004 08:41 Edited at: 13th Mar 2004 08:44
Although I finish off by proving that my length vector method is faster even the multiplication method using SQRT.

See:



My computer's results are:

SQRT multiplication method: 480
Length vector method: 117

And that, ladies and gentlemen, is as much as anyone will ever want to know about how to perform quick 3d distance calculations.

Now I'm off to do something more valuable with my life.

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home

Login to post a reply

Server time is: 2024-09-22 00:35:17
Your offset time is: 2024-09-22 00:35:17