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.

DarkBASIC Discussion / Finding distance between 2 points-

Author
Message
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 19th Jan 2004 19:40
I'm sure I saw something in the forums about working out the distance between 2 points..anyone remember?

YES, its for working out maths collision..

'Ooh 'eck chief'...'crumbs'
SteveK
21
Years of Service
User Offline
Joined: 24th Dec 2003
Location: US
Posted: 19th Jan 2004 21:21
pretty simple equation one for 3d and one for 2d if it doesnt work then tell me(im in a hurry)

I am the great cornholio. Take me to your TP!
GameKit
22
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 19th Jan 2004 21:22 Edited at: 19th Jan 2004 21:24
you use the distance formula...

Dist#=Sqrt((X1-X2)^2+(Y1-Y2)^2+(Z1-Z2)^2)

X1,Y1,and Z1 are the xyz positions of the first point,
X2,Y2,and Z2 are the xyz positions of the socond point...

well... i hope that helps...

[edit...]
SteaveK beat me to it... but i think his code gives you the distance cut in half...

Do not thwart the way of the dragon. For thou tasteth like chicken.
TKF15H
21
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 19th Jan 2004 21:23 Edited at: 19th Jan 2004 21:25
function Dist(x1,y1,z1,x2,y2,z2)
Distance#=SQRT(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2))+((z1-z2)*(z1-z2)))
endfunction Distance#

don't use ^2 as it's slower...

Why do programmers always mix up Christmas and Halloween?
Because DEC 25 = OCT 31
GameKit
22
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 19th Jan 2004 21:26
lol... SteveK,TKF15H,and I posted within the same 3 minutes...
I think all of our formulas work...

Do not thwart the way of the dragon. For thou tasteth like chicken.
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 19th Jan 2004 21:28 Edited at: 19th Jan 2004 21:32
I just wanted 2d but I'll jot down the 3d ones for later use...

thanx all..

although if someone could explain how you got the formulae..

Ie how the numbers interact with diagrams..maybe

I would appreciate that too.

'Ooh 'eck chief'...'crumbs'
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 19th Jan 2004 21:47
Ok I tried this to get me the distance between point A and the mouse pointer...but i get a cannot evaluate brackets error...

[b]distance# = sqrt((((mousex()-posx(n))/2^2)+(((mousey()-posy(n))/2^2))

hehe thats a mouthfull...

'Ooh 'eck chief'...'crumbs'
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 19th Jan 2004 22:02 Edited at: 19th Jan 2004 22:06
I just got

distance# = sqrt((posx(n)-mousex() )^2+(posy(n)-mousey() )^2)

to compile at the very least I hope it works

<<edited>>

nope don't think thats wrking giving me numbers 10 to 90 in increments of 10..??

'Ooh 'eck chief'...'crumbs'
GameKit
22
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 19th Jan 2004 22:09 Edited at: 19th Jan 2004 22:13
try...

Dist#=sqrt((mousex()-PosX)*(mousex()-PosX)+(mousey()-PosY)*(mousey()-PosY))

[edit...]
or
Dist#=sqrt((mousex()-PosX)^2+(mousey()-PosY)^2)
[end edit...]

the /2 will give you the wrong distance... and i don't know where you got the posx(n)...

well... i hope that fixed it...

Do not thwart the way of the dragon. For thou tasteth like chicken.
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 19th Jan 2004 22:15
my bad I didn't have the output display right I was trying to output to screen distance rather than distance#..

and the (n) is cuz its in a loop checking posx/y of several things

'Ooh 'eck chief'...'crumbs'
GameKit
22
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 19th Jan 2004 22:31 Edited at: 19th Jan 2004 22:38
oh, and as for a diagram...



hmmm... well... it's pretty sloppy... but i think you get the general idea.

Do not thwart the way of the dragon. For thou tasteth like chicken.
bibz1st
22
Years of Service
User Offline
Joined: 2nd Jan 2003
Location:
Posted: 19th Jan 2004 22:37
which is quicker,
^2 or SQRT cos I read somewhere that SQRT is slow
what about Dist#=(X1#-X2#)+(Y1#-Y2#)+(Z1#-Z2#)
kenmo
22
Years of Service
User Offline
Joined: 7th Sep 2002
Location:
Posted: 19th Jan 2004 23:59 Edited at: 27th Jun 2012 06:22
First of all ^2 is squared, which of course is the opposite of sqrt.

Second of all, your code would work, except it would return the distance, squared.
TKF15H
21
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 20th Jan 2004 02:30
no it wouldn't.
the origional formula (i think) was:
A^2=B^2+C^2

and form that we get:
A=sqrt(B^2+C^2)

which is NOT the same as:
A=B+C <----This is WRONG!!!

SQRT is slow, but it's necessary. Look for the distance only when
you really need it. I heard someone found a faster way using
vectors. Problem is, I don't know anything about DBP's vector commands. And DBC doesn't have vectors any way...

Why do programmers always mix up Christmas and Halloween?
Because DEC 25 = OCT 31
TKF15H
21
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 20th Jan 2004 02:30
no it wouldn't.
the origional formula (i think) was:
A^2=B^2+C^2

and from that we get:
A=sqrt(B^2+C^2)

which is NOT the same as:
A=B+C <----This is WRONG!!!

SQRT is slow, but it's necessary. Look for the distance only when
you really need it. I heard someone found a faster way using
vectors. Problem is, I don't know anything about DBP's vector commands. And DBC doesn't have vectors any way...

Why do programmers always mix up Christmas and Halloween?
Because DEC 25 = OCT 31
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Jan 2004 04:16
The proper equation looks like this.
c^2 = a^2 + b^2

"A" is the difference in the x values, and "b" is the difference in the y values. It's Pythagora's theorem.

"eureka" - Archimedes
Travis
21
Years of Service
User Offline
Joined: 9th Nov 2003
Location: West Aust.
Posted: 20th Jan 2004 05:08 Edited at: 20th Jan 2004 06:31
there is a quicker way using vectors that u should be able to do mathmatically in dbc, ill try to work it out as i go.... basically
ok


i hope they turn out ok...
in the first picture we can see the points a and c no if we get the difference in there x and y position we will end up with the side lengths in the second diagram...everyone following ?
now using simple trig. we can find one of the angles <A or <C ill chose A so:

tan A = 3/4 ... tan^-1 3/4 = 36.870°

then to find the hyp we go :

sin 36 = 3/X so X = 3 / sin 36 = 5

which makes the dist between A and C exactly 5
and if anyone knew that 3,4,5 is a pythagorian triple u would see that it worked (i hope i didnt stuff up)

only thing is that this might not be much quicker, if done this much now someone else can test that and seewhats faster. im guessing this is

#edit - i lied i dont think that thats really vector based ... vector based dist calcs. are exactly the same u find the difference in x and y then use pyth.
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 20th Jan 2004 06:39
@Travis, hmmm, maybe.

That would be about the same speed as the Distance formula,

@All, yes, the Sqrt() command is slow, but a simple way to get around it is,

(16)^(1/2) = 4
Sqrt(16) = 4

Putting something to the power of a half is the same as the square root, but it'll be quicker than DB's Sqrt() command.

Hope I Helped...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
Travis
21
Years of Service
User Offline
Joined: 9th Nov 2003
Location: West Aust.
Posted: 20th Jan 2004 06:42
omg why didnt i think of that ....*hits self in the head*
Penfold
21
Years of Service
User Offline
Joined: 3rd Dec 2003
Location: RED postbox houses of parliment
Posted: 20th Jan 2004 20:21
how about 16^0.5 = 4 that takes out 1 more maths function so would it be quicker?

'Ooh 'eck chief'...'crumbs'
Travis
21
Years of Service
User Offline
Joined: 9th Nov 2003
Location: West Aust.
Posted: 21st Jan 2004 05:22
yeah ... by the smallest amount its all based on how the comp does computations...
for example calculators and computers dont subtract they add negitive numbers so if u gave it 1-1=0 it would change it to 1+(-1)=0 so if u gave it the latter if would save a tiny bit of time the same thing happends with sqrt it changes it to 1/2 or 1/3 or whaterver and then even 5^2 has to be simplified to 5*5*5*5*5 then that back to 5+5+5+5+5+5......and i know that they change divide into multipy somehow so it all goes back to addition all the other buttons and functions are just shortcuts for the user

Login to post a reply

Server time is: 2025-05-22 14:20:58
Your offset time is: 2025-05-22 14:20:58