Find the distance to your target as a function of time. Once you know the time that your bullet will reach your target, you can calculate where he will be at that point in time and figure out the heading() to that position.
If you only know his initial position, this would be:
distance_to_target = sqr(target_x0² + target_y0²)
If you also know his velocity, it would be:
distance_to_target = sqr((target_x0 + t * target_vx)² + (target_y0 + t * target_vy)²)
Now find the distance to your bullet as a function of time:
distance_to_bullet = t * bullet_speed
Find their intersection:
distance_to_bullet = distance_to_target
t * bullet_speed = sqr((target_x0 + t * target_vx)² + (target_y0 + t * target_vy)²)
Solving for time, you get a polynomial after a bunch of factoring:
t² * (target_vx² + target_vy² - bullet_speed²) + t * (2 * (target_x0 * target_vx + target_y0 * target_vy)) + 1 * (target_vx² + target_vy²) = 0
Plugging those into the quadratic formula will give you the time(s) of intersection. As stated earlier, once you know the time that your bullet will reach your target, you can calculate where he will be at that point in time and figure out the heading() to that position.
Note that there are two answers to the quadratic formula! That's shown by the +/- symbol in the formula.
Sometimes there are two valid answers: if your target is doing a fly-by, you could fire at one angle and smoke him on his approach, or fire almost parallel to his trajectory and he'll catch up with the bullet after he passes you. It's also possible that the target is moving away from you at a speed greater than your bullet speed, in which case there will be no positive answers - you'd have to travel back in time to be able to hit him. If your target is travelling away from you at the same speed as your bullet, there are no valid answers. This will be reflected by a divide-by-zero in the quatratic formula - which is perfectly safe to do with floats, and will result in Infinity. I haven't thought about it too hard, but there's also the possibility that a square root of a negative number will be taken in the quadratic formula - this is also perfectly safe with floats and will result in NaN.
In summary, you'll want to choose the smallest positive root, being careful of special case floats such as NaN and +/-Infinity. You'll also need logic to take care of cases where there is no valid answer (does the enemy just fire at a random angle, always fire due North, or wait?)
Note that the target's position and velocity should be relative to your own.
DISCLAIMER: I haven't checked my math; you should derive the formulae yourself to make sure it's correct.
If you only know his initial position, this would be:
distance_to_target = sqr(target_x0² + target_y0²)
If you also know his velocity, it would be:
distance_to_target = sqr((target_x0 + t * target_vx)² + (target_y0 + t * target_vy)²)
Now find the distance to your bullet as a function of time:
distance_to_bullet = t * bullet_speed
Find their intersection:
distance_to_bullet = distance_to_target
t * bullet_speed = sqr((target_x0 + t * target_vx)² + (target_y0 + t * target_vy)²)
Solving for time, you get a polynomial after a bunch of factoring:
t² * (target_vx² + target_vy² - bullet_speed²) + t * (2 * (target_x0 * target_vx + target_y0 * target_vy)) + 1 * (target_vx² + target_vy²) = 0
Plugging those into the quadratic formula will give you the time(s) of intersection. As stated earlier, once you know the time that your bullet will reach your target, you can calculate where he will be at that point in time and figure out the heading() to that position.
Note that there are two answers to the quadratic formula! That's shown by the +/- symbol in the formula.
Sometimes there are two valid answers: if your target is doing a fly-by, you could fire at one angle and smoke him on his approach, or fire almost parallel to his trajectory and he'll catch up with the bullet after he passes you. It's also possible that the target is moving away from you at a speed greater than your bullet speed, in which case there will be no positive answers - you'd have to travel back in time to be able to hit him. If your target is travelling away from you at the same speed as your bullet, there are no valid answers. This will be reflected by a divide-by-zero in the quatratic formula - which is perfectly safe to do with floats, and will result in Infinity. I haven't thought about it too hard, but there's also the possibility that a square root of a negative number will be taken in the quadratic formula - this is also perfectly safe with floats and will result in NaN.
In summary, you'll want to choose the smallest positive root, being careful of special case floats such as NaN and +/-Infinity. You'll also need logic to take care of cases where there is no valid answer (does the enemy just fire at a random angle, always fire due North, or wait?)
Note that the target's position and velocity should be relative to your own.
DISCLAIMER: I haven't checked my math; you should derive the formulae yourself to make sure it's correct.