Demonstrates how to do 2D and 3D distance checks using vectors.
Use it to test distance between two objects, camera and an object, two points in space, etc.
sync on : sync rate 60 : autocam off
rem you need to set these up
null2D=make vector2(2)
null3D=make vector3(3)
make object cube 1,10
make object cube 2,10
make matrix 1,500,500,32,32
position matrix 1,-250,0,-250
position object 1,-10,5,20
position object 2,10,20,20
position camera 0,5,-20
do
control camera using arrowkeys 0,2,2
set cursor 0,0
x1#=object position x(1)
y1#=object position y(1)
z1#=object position z(1)
x2#=object position x(2)
y2#=object position y(2)
z2#=object position z(2)
cx#=camera position x()
cy#=camera position y()
cz#=camera position z()
print "2D distance between objects = ";range2D(x1#,z1#,x2#,z2#)
print "3D distance between objects = ";range3D(x1#,y1#,z1#,x2#,y2#,z2#)
print
print "2D distance between camera and object 1 = ";range2D(cx#,cz#,x1#,z1#)
print "3D distance between camera and object 1 = ";range3D(cx#,cy#,cz#,x1#,y1#,z1#)
print
print "2D distance between camera and object 2 = ";range2D(cx#,cz#,x2#,z2#)
print "3D distance between camera and object 2 = ";range3D(cx#,cy#,cz#,x2#,y2#,z2#)
sync
loop
rem distance between 2 positions in 2D space
function range2D(ax#,az#,bx#,bz#)
set vector2 2,ax#-bx#,az#-bz# : result#=length vector2(2)
endfunction result#
rem distance between 2 positions in 3D space
function range3D(ax#,ay#,az#,bx#,by#,bz#)
set vector3 3,ax#-bx#,ay#-by#,az#-bz# : result#=length vector3(3)
endfunction result#
Boo!