Quote: "do you have a small example how to check the array?"
I came up with something that is better and faster, using vector3. The following code creates 600 cubes and places them randomly. You then can move the camera using the arrowkeys. The code checks for the nearest cube and reports it. You can move around pretty fast and it updates the nearest one faster than you can read it.
` example: checking arrays to see if an object is present
` LBFN 7 June, 2010
sync on : sync rate 100
autocam off : backdrop on : color backdrop 0
randomize timer() : hide mouse
global Obj as integer : global nearest as integer
global TotalObj as integer : TotalObj = 600
#constant true = 1
#constant false = 0
null = make vector3(1)
` make 600 tower pieces, starting at 500
MakeTower(500)
repeat
control camera using arrowkeys 0,2.0,1.5
nearest = CheckNearest(camera position x(),camera position y(),camera position z())
debug()
sync
until spacekey() = true
for g = 500 to (500 + TotalObj)
if object exist(g) = true then delete object g
next g
show mouse
end
function MakeTower(obj)
` okay, it's not really a tower, but you can use it to check for position
LastObj = obj + TotalObj
repeat
` make 600 cubes and place them randomly
make object cube obj,rnd(5) + 2
position object obj,rnd(500) - 250,0,rnd(500) - 250
color object obj,rgb(rnd(128) + 127,rnd(128) + 127,rnd(128) + 127)
inc obj
until obj > LastObj
endfunction
function CheckNearest(x1#,y1#,z1#)
` enters with the x, y and z coordinates of the camera to check proximity with
nearest = 0 : distance# = 5000.0
for g = 500 to (500 + TotalObj)
` store the current object's x,y and z positions
x2# = object position x(g)
y2# = object position y(g)
z2# = object position z(g)
` get the length of the vector
set vector3 1, x1# - x2#, y1# - y2#, z1# - z2#
dist# = length vector3(1)
`if this object is closer than any of the others, then update the distance and save the object #
if dist# < distance# then distance# = dist# : nearest = g
next g
` outputs the nearest object to the camera
endfunction nearest
function debug()
` provide debug info
ink rgb(255,255,0),0
text 10,10,"FPS: " + str$(screen fps() )
text 10,100,"nearest: " + str$(nearest)
endfunction
I hope this is helpful.
LBFN