

ok, so this question should be in the Math forum and is not really tes specific, but it's been a loooooooooooooooong time since I last went to school and I'm to lazy to search the dark recesses of my mind for the correct way to do this...
here we go:
in a three dimensional coordinate system, how do I calculate the distance between 2 points?
I just need the formula...
by the way, if you wonder why I need to do this: it's for the GetDistance function...
Post Extras:

ManaUser

Master



Reged: 05/31/00

Posts: 6115

Loc: Long Beach, CA, USA



I guess you can just use the Pythagorean Theorem twice.
So, if I'm right that would be something like this in Blight Script:
Code:
float A float B float H
set A to ( MyX  YourX ) ;assuming those are already set. set B to ( MyY  YourY ) ;assuming those are already set.
set A to ( A * A ) set B to ( B * B ) set C to ( A + B ) set C to GetSquareRoot C ;C is now the 2D distance
set A to C set B to ( MyZ  YourZ ) ;assuming those are already set.
set A to ( A * A ) set B to ( B * B ) set C to ( A + B ) set C to GetSquareRoot C ;C is now the 3D distance
Post Extras:

Graphite

Acolyte



Reged: 11/07/03

Posts: 161




Same calculation, just shorter (if you don't believe me, just write it all out and you'll see that you can just take the square root of all the squares in one go):
Code:
set dx to x2  x1
set dy to y2  y1
set dz to z2  z1
set dist to ( dx * dx ) + ( dy * dy ) + ( dz * dz )
set dist to GetSquareRoot dist
Post Extras:

Simpleton

Acolyte



Reged: 07/02/04

Posts: 138

Loc: Earlham College, Richmond, IN



EDIT: bah, fine, steal my glory, I don't care.
almost manauser. All you need to do is find the difference between the points on all 3 axis and then use the pythag. theorem to get the distance.
so if given (X1,Y1,Z1) and (X2,Y2,Z2) just do the following:
sqrt( (X1  X2)^2 + (Y1  Y2)^2 + (Z1  Z2)^2 )
sqrt mean square root
and ^2 means squared
oh and when you code it be careful about how you do the calculations because sometimes the game doesn't like overly complex computations on a single line.
Hope this works for ya
 Do you have a burning desire to give me money?
Click Here to Donate
Edited by Simpleton (08/24/04 05:58 PM)
Post Extras:

Mode_Locrian

Diviner



Reged: 10/07/02

Posts: 2084

Loc: Bjornholm, Rykith Lowlands Region



Good old Pythogoras. A^2*B^2=C^2. Nice work guys.
 My Website
Bards of Vvardenfell Thread (New Info 8/15/04)
Post Extras:

ManaUser

Master



Reged: 05/31/00

Posts: 6115

Loc: Long Beach, CA, USA



Now I'm a little confused, is sqrt( X^2 + Y^2 + Z^2 ) equivalent to sqrt( sqrt( X^2 + Y^2 ) + Z^2 )? Because I tried it on my calculator and it doesn't seem to give the same result. If not, which is really right? I maybe just be doing it wrong of course.
Post Extras:

Simpleton

Acolyte



Reged: 07/02/04

Posts: 138

Loc: Earlham College, Richmond, IN



I may be mistaken but I'm pretty sure they are different, and I'm pretty sure the first equation is the correct one.
EDIT: after going through it again in my head they are close to being the same. You are doing sqrt( xylength + z^2 ) when you meant to do sqrt( xylength^2 + z^2 ) which if you think about it is r^2 = x^2 + y^2 + z^2 (r being the length), which is the 3d pythag theorem
 Do you have a burning desire to give me money?
Click Here to Donate
Post Extras:

Grumpy

Disciple



Reged: 08/02/02

Posts: 1590




Don't confuse me with these others above. I have no idea how this works, but it does...
get the x,y,z coords of the ogrm, then get the x,y,z coords of the player and do:
Code:

; x_o, y_o, z_o are ogrim coords
; x_p, y_p, z_p are player coords
; distance = sqrt( (x_o  x_p)^2 + (y_o  y_p)^2 + (z_o  z_p)^2 )
set temp to x_o  x_p
set temp to temp * temp
set temp2 to y_o  y_p
set temp2 to temp2 * temp2
set temp3 to z_o  z_p
set temp3 to temp3 * temp3
set temp to temp + temp2 + temp3
set distance to ( GetSquareRoot, temp )

That was the original one that Simpleton posted a month or so ago. Been using it on my companion for about as long, and it's working just fine.

float ax
float ay
float bx
float by
float bz
float t1
float t2
float coDist
set ax to ( Player>GetPos x ) ;these two get set every frame
set ay to ( Player>GetPos y )
if ( doOnce == 0 )
set bx to ( Player>GetPos x ) ;these get set one time
set by to ( Player>GetPos y )
set bz to ( Player>GetPos z )
set doOnce to 1
endif
set t1 to ( ax  bx )
set t1 to ( t1 * t1 )
set t2 to ( ay  by )
set t2 to ( t2 * t2 )
set t1 to ( t1 + t2 )
set coDist to ( GetSquareRoot, t1 ) ;difference between the two
if ( coDist >350 )
do something
set doOnce to 0 ;restarts the sequence

Way I've been using it if it helps any.
Again, many thanks to Mr. Simpleton...
 Grumpy
My mods
Edited by Grumpy (08/24/04 09:21 PM)
Post Extras:

ManaUser

Master



Reged: 05/31/00

Posts: 6115

Loc: Long Beach, CA, USA



Quote:
You are doing sqrt( xylength + z^2 ) when you meant to do sqrt( xylength^2 + z^2 ) which if you think about it is r^2 = x^2 + y^2 + z^2 (r being the length), which is the 3d pythag theorem
D'oh. Okay, so my code works, I just did it wrong on the calculator. The r^2 = x^2 + y^2 + z^2 version is still simpler of course.
Post Extras:
