# Thread: Help with hitting a moving target

1. Member
Join Date
Jan 2012
Posts
18
Rep Power
0

## 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?

2. Member
Join Date
Jan 2012
Posts
18
Rep Power
0

## Re: Help with hitting a moving target

Nevermind, i got it. Maybe not precise, but it looks pretty good to me.

3. Member
Join Date
Jan 2012
Posts
18
Rep Power
0

## 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?

4. ## 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?

5. Member
Join Date
Jan 2012
Posts
18
Rep Power
0

## 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.

6. Member
Join Date
Oct 2012
Location
ph
Posts
43
Rep Power
0

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•