#Calculate the time when we can hit a target with a bullet
#Return a negative time if there is no solution
function number aimAhead([Vr Delta]:vector, MuzzleV)
{
	#Quadratic equation coefficients a*t^2 + b*t + c = 0
	local A = Vr:dot(Vr) - MuzzleV^2
	local B = 2 * Vr:dot(Delta)
	local C = Delta:dot(Delta)

	local Det = B*B - 4*A*C

	#If the determinant is negative, then there is no solution
	if(Det > 0)
	{
		return 2*C/(sqrt(Det) - B)
	}else
	{
		return -1
	}
}

#Find the relative position and velocities
Delta = Target:pos() - Gun:pos()
Vr = Target:vel() - Gun:vel()

#Calculate the time a bullet will collide if it's possible to hit the target.
T = aimAhead(Delta, Vr, MuzzleV)

#If the time is negative, then we didn't get a solution.
if(T > 0)
{
	#Aim at the point where the target will be at the time
	#of the collision.
	AimPoint = Target:pos() + T * Vr
}