function length(x, y, z)
return math.sqrt((x ^ 2) + (y ^ 2) + (z ^ 2))
end
function normalize(x, y, z)
local length = length(x, y, z)
if length > 0 then
return {x = x / length, y = y / length, z = z / length}
else
return {x = 0, y = 0, z = 0}
end
end
function getDistance3D(A, B)
return length(A.x - B.x, A.y - B.y, A.z - B.z)
end
local pos = {x = 100, y = 1500, z = 10}
local destinationPos = {x = 1000, y = 880, z = 1000}
local vector = {x = 0, y = 0, z = 0}
local step = 100
local dist = 1
while getDistance3D(pos, destinationPos) > dist do
if step > getDistance3D(pos, destinationPos) then
pos = destinationPos
end
vector = normalize(pos.x - destinationPos.x, pos.y - destinationPos.y, pos.z - destinationPos.z)
pos = {x = pos.x - vector.x * step, y = pos.y - vector.y * step, z = pos.z - vector.z * step}
print(pos.x, pos.y, pos.z, getDistance3D(pos, destinationPos))
end
print('END')