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?
Printable View
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?
Nevermind, i got it. Maybe not precise, but it looks pretty good to me.
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?
Not really sure what you're actually asking. What do you do now? Why does it not look like they're hitting the 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.
why dont you use something like direction, like if( position of object == postion of bullet) object will be destroyed.