Purpose of calculation in Tower Defense

I am building a little tower game with some friends in Java. Now I have been assigned logic for the towers, and for now I’m trying to figure out how the tower should turn in order to aim and hit the target monster. As the monster moves when the tower turns and shoots, it should aim for a future position. I implemented a function that gives me the position of the monster at any time t, as well as a function that gives me the smaller angle needed to access the monster, but now I'm confused because there are three unknown variables:

  • t1 or angle: time or angle, the tower must be rotated (the speed with which the tower can rotate)
  • t2 or firing distance: the time when the bullet must move to hit the target (speed is also indicated, constant).
  • t3 or travel distance: the distance the monster moves at the same time.

So, I am looking for a solution for:

min(t1+t2) = min(t3) 

where the target monster is still within range of the tower. I already thought about calculating with the maximum necessary rotation and the maximum possible range, and then with a step-by-step decrement, but I am curious if there is a β€œperfect” non-heuristic solution?

+4
source share
2 answers

ADDED INFORMATION:

I assume that this monster has a distance D to the tower, moves along the shortest path to the tower, and the tower begins to turn towards the monster. This is the situation at t=0 .

FIXED TYPES:

If your tower rotates at angular omega , that is, the angle phi at time t is

 phi = omega * t 

So, if you know that your tower should rotate the phi angle, the bullet will shoot at

 t = phi/omega 

From this, at a distance traveled by bullet v ,

 s(t) = v * (t-phi/omega) 

If your monster moves at vm , the monster will be at a distance d

 d(t) = D - vm * t 

A bullet hits a monster if

 s(t) = d(t) 

This equation is easy to solve: just replace d(t) and s(t) and rearrange the terms to get t :

 t = (D + v * phi/omega) / (phi/omega + vm) 

And the bullet will pass s(t) at that moment. If this value is negative, the monster was too fast and reached the tower before the bullet was fired.

+3
source

For each coordinate, we can calculate the time t B that we need to have a bullet in this place. That turn time plus the time required by bullets (t 1 + t 2 ).

The earliest time we can hit a monster is the first place on the monsters' path (predicted), where the monster and the bullet have their fatal encounter.

I would start from the place of monsters, calculate the times and places for the beast and figure out whether the bullet will arrive sooner or later.

You can eliminate one of the variables if you simply turn and check each degree or mark if you can kill a monster if you shoot now. You will have only two vectors (target, direction of movement of monsters) with one intersection point and you just need to check if the monster and the bullet meet there at the same time (distance to the intersection point, speed).

+4
source

All Articles