Notifications
Clear all

[Closed] how do you calculate the distance?

hi there…
i found this to calculate the distance between two points at www.mathworld.com

and then tried it on maxscript using as:
sqrt((2^($Box02.position.x – $Box01.position.x)) – (2^($Box02.position.y – $Box01.position.y)))

but the result is allways 0 🙁
whats wrong? how do you calculate the distance?
thanks for help:thumbsup:

4 Replies

 distance $box01.pos $box02.pos
 

You’ve got the quares wrong – it is (B-A)^2, not 2^(B-A)…
Also, the example is for 2D distance only, you need the 3rd component, and you have to ADD all 3 squares before you calculate the Square root, not subtract them.

sqrt((($Box02.pos.x – $Box01.pos.x)^2) + (($Box02.pos.y – $Box01.pos.y)^2) + (($Box02.pos.z – $Box01.pos.z)^2) ) –full form

distance $Box01.pos $Box02.pos –as already mentioned

–when working with nodes, MXS is smart enough to know what you want – this is the short-hand form:
distance $Box01 $Box02

 eek

also you can make this even cleaner using a for loop. Well its just this:

 
 
r = 0
for i = 1 to 3 do
(
r = r + (abs(p0.transform.pos[i]-p1.transform.pos[i]))^2
)
 
sqrt(r)
 

This will use point2 values also if you set the 3 in the for loop to a 2.

thank u so much friends:thumbsup:
distance is the easiest way of course but i didnt know:)