See my image below:
The black-right-arrow is my ship and the red circle my enemy (radius 1.2). Both are in movement without acceleration. I need kill the red circle with precision, what type of calculations can I use with only linear velocity, position, and radius data?
Note: The bullet has 0.5s of lifetime and the speed 40 and radius 0.2, and I can't change the speed of the bulet, only change the shoot direction.
How to can I calcule the shoot direction to precise kill the red circles?
I tried in C#:
public static Vector3 CalculatePreciseShootDirection(Vector3 shipPosition, Vector3 shipVelocity,
Vector3 targetPosition, Vector3 targetVelocity,
float bulletLifetime, float bulletSpeed)
{
Vector3 shipFuturePosition = shipPosition + bulletLifetime * shipVelocity;
Vector3 targetFuturePosition = targetPosition + bulletLifetime * targetVelocity;
Vector3 relativeDisplacement = targetFuturePosition - shipFuturePosition;
float timeToTarget = Vector3Magnitude(relativeDisplacement) / bulletSpeed;
Vector3 adjustedTargetPosition = targetPosition + timeToTarget * targetVelocity;
Vector3 shootingDirection = adjustedTargetPosition - shipPosition;
float shootingDistance = Vector3Magnitude(shootingDirection);
float maxDistance = bulletLifetime * bulletSpeed;
if (shootingDistance <= maxDistance)
{
return Vector3.Normalize(shootingDirection);
}
else
{
return Vector3.Zero;
}
}
private static float Vector3Magnitude(Vector3 v)
{
return (float)Math.Sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);
}
But always I get Vector3.Zero, what is wrong in my function?

First step: change the coordinate system so that you are stationary at the origin $(0,0)$ and calculate how the target is moving relative to you.
Second step: for each possible direction, find the time at which a bullet fired in that direction crosses the line the target is following (in terms of the unknown angle). Then find the time at which the target reaches that point.
Finally: equate the two times in the second step. That will give you one equation in the unknown angle in terms of the known input data.
There will be situations where you can't hit the target because the bullet is too slow.
This is all pretty much elementary algebra.