Reputation: 135
I am currently programming a tower defense and the missiles tend to miss the enemy instead of following/hitting them like they do in desktop tower defense. This is because they shoot without giving the enemy any lead so by the time it reaches the enemy position, the enemy is already 3-5 pixels away. I looked up on how to fix this and found that I need to use vector math to resolve the problem. This site http://www.helixsoft.nl/articles/circle/sincos.htm for example has some code on how to program homing missiles, but I am uncertain whether this is the kind of math that I need to resolve my problem.
seems like the posts on stackoverflow recommend something called "Command Guidance" but have no idea how that would work with a 2d game.
so at the moment, im pretty confused as to what to do. Any direction/guidance would be much appreciated.
Upvotes: 0
Views: 1321
Reputation: 21047
You need to "predict" the direction of your enemy. Assuming that it moves along a straight line, the tower needs to observe the speed and direction of it's target.
Let's say your tower is in the point (0,0) and you collect two observations of your target:
t=0
t=1
Speed: First calculate the distance between this two points:
d = sqrt((x2-x1)^2 + (y2 - y1)^2) =
= sqrt(10^2 + 5^2) = sqrt(125) = 11.18034
So the speed of your target is 11.18034 (since you took the observations in an interval equivalent to one time-unit)
Angle: A bit of geometry. The slope of the trajectory is:
m = (y2 - y1) / (x2 - x1) =
= 5 / 10 = 0.5
So the angle is:
theta = arctan(0.5) = 0.463648 radians (or 26.56 degrees)
With two points and the slope, you can estimate the trajectory of your target:
y - y1 = m * (x - x1)
==> y = 0.5 * (x - 100) + 100 =
= 0.5 * x + 50
All that remains is to calculate the point where your missile can intercept the target. For that you'll need to know the speed of your missile and then calculate the "optimal" interception point. I'll leave you this second step to you. It's simple geometry (and a bit of creativity)
Upvotes: 1
Reputation: 12742
It sounds like you already have code that would hit the enemies correctly if they were stationary, and only that fact that they move while the missile is in flight is the issue.
Why don't you simply "relaunch" the missile from its current position towards the new position of the enemy whenever the enemies take a step, or every 10 pixels or so that the missile moves.
This would lead to the missile "homing" in on the enemy in flight, rather than predicting where the enemy will be at the time that the missile should arrive.
The difference is that the "homing" should lead to a hit every time, whereas the predictive approach will allow the enemies to dodge the missile by switching walking direction while the missile is in flight. Which one you prefer is a design decision, but I think the usual tower defense games use the homing approach.
Upvotes: 1
Reputation:
That article is exactly what you need, read the section on homing missiles and just use the inverse tangent function.
Upvotes: 1