Distance between two points in 3d.

Blitz3D Forums/Blitz3D Programming/Distance between two points in 3d.

Farflame(Posted 2003) [#1]
Fairly basic question, but I need to find the distance between two points in 3d, using software. As far as I'm aware, the formula is :

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

But it isn't working in my program. I've tried putting brackets around (xd*xd)+(yd*yd)... etc, that doesn't work either. I am using very, very large numbers, as high as 1,000,000,000, but should that make a difference? Is the formula correct?


CyBeRGoth(Posted 2003) [#2]
Function DISTANCE3D#(x1#,y1#,z1#,x2#,y2#,z2#)
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; USE EUCLIDEAN DISTANCE (3D) TO WORK OUT THE DISTANCE TWO ENTITIES ARE FROM EACH OTHER
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DISTANCE#=Sqr((X2-X1)^2+(Y2-Y1)^2+(Z2-Z1)^2)

Return DISTANCE
End Function


Should do the trick


Michael Reitzenstein(Posted 2003) [#3]
That is exactly what he is doing, except substantially slower - using ^ for squaring a number is significantly slower than standard multiplication. I believe your problem, Farflame, is that you are not using floats?


CyBeRGoth(Posted 2003) [#4]
Is it a lot slower? I think I will change to Multiplication like you suggest then.

I too think the problem might be floating point numbers, or perhaps your entities are children of other entities? In which case you will need to use the global command when getting their x,y,z like

x1#=entityx#(spaceship,1)


Farflame(Posted 2003) [#5]
Ahh yes, I was using some floats but a couple of my variables were integers, so it was messing it up.

Out of interest, what's the range of an integer and a float? I can't find that info in the manual/help files.


dangerdave(Posted 2003) [#6]
It's in the language description in the help.

Both types are 4 bytes/32bit.
So you can easily get out of range if you do a lot of values
that are 1000000000.


Farflame(Posted 2003) [#7]
Found it. So what happens if I want to use larger values? Doesn't Blitz have anything bigger, such as a long value?


jfk EO-11110(Posted 2003) [#8]
Unfortunately No. But I hope it will be in BlitzMAX. And I guess somebody wrote a 64 Bit Workaround in the Code Archives, no? (EDIT: no, sorry, I confused it) Well I never needed it.


sswift(Posted 2003) [#9]
far:
Blitz's integers ARE longs. They can go up to approximately +-2 billion.

Blitz's floats can go really really high. I can't really say how high for sure though. But the problem is that the higher or lower you go with a float, the less precise the value is.

In other words, if you have a huge float, like 10 billion, and you add 0.1 to it, it might still be 10 billion, and not 10 billion + 0.1.

With floats what you need to know is the number of digits of precision they have, but I don't know what it is in Blitz. But it's good enough for most realistic sizes of level. I use a scale of 1 unit = 1 meter, and I never have levels that are more than 2km or so wide, and I've never had a problem, but if you're modelling space, then you might have a problem if you don't scale everything down.


Farflame(Posted 2003) [#10]
Hmm, if floats can handle larger numbers, then they might be better for my game, since precision isn't critical, nor is speed (well it is, but I'd rather have the larger numbers). It might just be wiser to scale everything down a bit, but I found that if I scale my ships down too much, they start to distort, so I'm a bit limited.


Jager(Posted 2003) [#11]
Why not use EntityDistance#()? or have I missed something?