I hope this helps:
Type otherObject
id as integer
x as float
y as float
z as float
endType
Sync on
Sync rate 60
` Create an array to hold all the target objects
Dim objects(-1) as otherObject
` Create some objects at a random position
range# = 1000
for i=1 to 100
array insert at bottom objects()
objects().id = i
objects().x = rnd(range#)
objects().y = 0
objects().z = rnd(range#)
make object box objects().id, 10, 10, 10
position object objects().id, objects().x, objects().y, objects().z
next i
target = -1
objectToMove = -1
` Position the camera in the air, pointing down at the map
position camera range#/2, 1000, range#/2
point camera range#/2, 0, range#/2
` Set the object you want to move
objectToMove = 0
set object diffuse objects(objectToMove).id, rgb(0, 255, 0)
id = objects(objectToMove).id
Do
if spacekey()
if target = -1
` Find the closest object
target = FindClosestObject(object position x(id), object position y(id) ,object position z(id), objectToMove)
endif
endif
set cursor 0, 0
print "Fps: ", screen fps()
print "Target: ", target
if target > -1
` Move the main object to the target object
if MoveObjectToPoint(objects(objectToMove).id, objects(target).x, objects(target).y, objects(target).z, 20)
` Delete target object if the main object arrived
delete object objects(target).id
array delete element objects(), target
if target < objectToMove then dec objectToMove
target = -1
endif
endif
Sync
Loop
Function FindClosestObject(x#, y# ,z#, ignoreObject)
closestDist# = 999999
index = -1
` Loop through the array holding the objects
objectCount = array count( objects() )
for i=0 to objectCount
` Get the distance from the target point
dx# = x# - objects(i).x
dy# = y# - objects(i).y
dz# = z# - objects(i).z
dist# = sqrt(dx#*dx# + dy#*dy# + dz#*dz#)
` Ignore the object set by the user
if i<>ignoreObject
` Check if the object is closer then the object already picked
if dist# < closestDist#
closestDist# = dist#
index = i
endif
endif
next i
if index>-1 then set object diffuse objects(index).id, rgb(255, 0, 0)
endFunction index
Function MoveObjectToPoint(obj, x#, y#, z#, speed#)
arrived = 0
` Get the distance from the target point
dx# = object position x(obj) - x#
dy# = object position y(obj) - y#
dz# = object position z(obj) - z#
dist# = sqrt(dx#*dx# + dy#*dy# + dz#*dz#)
` If the distance is smaller then the speed we need to position the object at the target position
if dist# < speed#
position object obj, x#, y#, z#
arrived = 1
else
` If it isn't we need to move it to the target position
` First store the angle of the object
currentAngleX# = object angle x(obj)
currentAngleY# = object angle y(obj)
currentAngleZ# = object angle z(obj)
` Then point the object to the target position
point object obj, x#, y#, z#
` Move the object with the speed
move object obj, speed#
` Rotate the object back to his original rotation
rotate object obj, currentAngleX#, currentAngleY#, currentAngleZ#
endif
endFunction arrived