I'm building a little tower-defense game with some friends in Java. Now I'm assigned with the logic for towers and at the moment I'm trying to figure out how a tower has to turn to aim and hit a target monster. Because a monster moves on while the tower is turning and shooting, it needs to aim for a future position. I've implemented a function, which gives me the position of a monster at any time t and also a function, which gives me the smaller angle needed to turn to a monster, but now I'm confused because there are three unknown variables:
- t1 or angle: the time or angle, the tower needs to turn (the speed at which a tower can turn is given)
- t2 or shoot distance: the time a bullet needs to travel to hit the target (speed also given, constant).
- t3 or travel distance: the distance the monster travels in the same time.
So I'm searching for a solution for:
min(t1+t2) = min(t3)
where the target monster is still in range of the tower. I already thought of calculating with the maximal needed turn and maximal possible range and then stepwise decrementing, but I'm curious if there is a "perfect" non-heuristic solution?