# Help with hitting a moving target

• 05-31-2012, 01:32 AM
waker3210
Help with hitting a moving target
I'm working on a tower defense type game and I want to know how to make the missiles look like they are actually hitting the target. What is the best way to do this?
• 05-31-2012, 06:27 AM
waker3210
Re: Help with hitting a moving target
Nevermind, i got it. Maybe not precise, but it looks pretty good to me.
• 05-31-2012, 03:11 PM
waker3210
Re: Help with hitting a moving target
So it turns out i don't have it. What i did somehow managed to work, but only in one case. So, any ideas how to do this right?
• 05-31-2012, 03:49 PM
KevinWorkman
Re: Help with hitting a moving target
Not really sure what you're actually asking. What do you do now? Why does it not look like they're hitting the target?
• 06-01-2012, 05:01 PM
waker3210
Re: Help with hitting a moving target
Well, I thought I could just find the distance from the bullet to the enemy and then get the time it would take to reach it at that distance. Then aim at where the enemy is, added to the time multiplied by the enemies constant velocity. That solution was effective because it was dealing with relatively short distances, but it really isn't very accurate.

So in mathematical terms:
(Please note that this is not workable code)

I used the distance formula to find the distance from the bullet to the enemy:
d = Math.sqrt(Math.pow(enemy.x - bullet.x,2) + Math.pow(enemy.y - bullet.y,2))

Then I find the time it takes the bullet to reach the enemy based on that distance:
t = d/bullet.speed

Then knowing that, the displacement of the enemy would be t*enemy.speed.

I then add the displacement to the enemies current position to find out where to shoot.

This is not entirely accurate, but it would be okay except that when the enemies change direction it sometimes messes up. Not really sure why, it's probably something wrong in another part of my code. But ultimately i would like to find a better solution.
• 12-26-2012, 07:06 AM
nonybrighto
Re: Help with hitting a moving target
why dont you use something like direction, like if( position of object == postion of bullet) object will be destroyed.