function VectorMovementCollision(startPoint1, endPoint1, v1, startPoint2, v2, delay) local sP1x, sP1y, eP1x, eP1y, sP2x, sP2y = startPoint1.x, startPoint1.z or startPoint1.y, endPoint1.x, endPoint1.z or endPoint1.y, startPoint2.x, startPoint2.z or startPoint2.y --v2 * t = Distance(P, A + t * v1 * (B-A):Norm()) --(v2 * t)^2 = (r+S*t)^2+(j+K*t)^2 and v2 * t >= 0 --0 = (S*S+K*K-v2*v2)*t^2+(-r*S-j*K)*2*t+(r*r+j*j) and v2 * t >= 0 local d, e = eP1x-sP1x, eP1y-sP1y local dist, t1, t2 = math.sqrt(d*d+e*e), nil, nil local S, K = dist~=0 and v1*d/dist or 0, dist~=0 and v1*e/dist or 0 local function GetCollisionPoint(t) return t and {x = sP1x+S*t, y = sP1y+K*t} or nil end if delay and delay~=0 then sP1x, sP1y = sP1x+S*delay, sP1y+K*delay end local r, j = sP2x-sP1x, sP2y-sP1y local c = r*r+j*j if dist>0 then if v1 == math.huge then local t = dist/v1 t1 = v2*t>=0 and t or nil elseif v2 == math.huge then t1 = 0 else local a, b = S*S+K*K-v2*v2, -r*S-j*K if a==0 then if b==0 then --c=0->t variable t1 = c==0 and 0 or nil else --2*b*t+c=0 local t = -c/(2*b) t1 = v2*t>=0 and t or nil end else --a*t*t+2*b*t+c=0 local sqr = b*b-a*c if sqr>=0 then local nom = math.sqrt(sqr) local t = (-nom-b)/a t1 = v2*t>=0 and t or nil t = (nom-b)/a t2 = v2*t>=0 and t or nil end end end elseif dist==0 then t1 = 0 end return t1, GetCollisionPoint(t1), t2, GetCollisionPoint(t2), dist end local A = { x = 1, y = 5} -- Alice's initial position local B = { x = 4,y = 1 } -- Rabbit's initial position local H = { x = 6, y = 7 } -- Hole's coordinates local Sa = 1.1 -- Alice's speed local Sb = 1.0 -- Rabbit's speed local t1, p1, t2, p2 = VectorMovementCollision(B, H, Sb, A, Sa, 0) if t1 and p1 then print("Time: " .. t1 .. " Position {" .. p1.x .. "," .. p1.y .. "}") end if t2 and p2 then print("Time: " .. t2 .. " Position {" .. p2.x .. "," .. p2.y .. "}") end