# aim at moving target algorithm

## Recommended Posts

Hello, I am currently finetuning a 2d topdown shooter and want to implent that the ki can aim at the expected future position of a moving target. Which is a really cool thing to have btw(Im using a dirty approximation at the moment). Unfortunately I havent been able to find an exact formula so far. We know that shot and target will meet if the shot is set off at the right angle. We know the startposition of our shot. We know the speed of our shot. We know the actual position of the target. We know the angle/direction(up is zero) in which the target is moving. We know the speed at which the target is moving. All speeds and angles are constant. And we want to know the angle in which we have to shoot to hit. Or the point the target and shot will meet if we shoot at the right angle. Help appreciated. My thoughts so far (if anyone cares): Using linear functions (y = xm + n) I could calculate the number_of_frames it takes for the two objects to hit if the shot was done at the right angle. number_of_frames = n/(m_two - m_one); The rest would be easy. But I would have to know the speed at which the distance between the two objects is shrinking which I think I cant without knowing the angle at which we would have to shoot. So my idea is probably useless. Thank you

##### Share on other sites
jyk    2094
This has been covered quite a few times in this particular forum. Try searching the forum archives for 'lead target', 'moving target', and maybe other similar phrases, and you should find some good explanations of the algorithm (and maybe even some sample code).

##### Share on other sites
Thank you, Ive got enough resources to figure this out now with some study.