3

I'm creating a game in a grid (like Chess, x/y coordinates) on this game there is some projectiles which have to hit moving targets.

Moving targets follow a path (get with an A* pathfinding) composed by X steps of X/Y coordinates. Projectile don't follow a path, they are going straight from the sender to the target.

I would like to calculate the coordinates where the projectile will hit the target (according to his path)

I tried to use: 2d game : fire at a moving target by predicting intersection of projectile and unit but this algorithm does not work if the target turn during the projectile fly.

Can you help me with this algorithm?

As input I have:

  • Cordinates of each step of the target path
  • Velocity of target & projectile
  • Actual x/y target position (It can be a float like x:7.61, y:5.22 if needed)
  • Position of projectile sender

You can consider that:

  • The target never change his path during a projectile fly
  • If the target is not moving, the velocity is equal to 0
  • The projectile start can be delayed if needed

Thanks for any help!

Community
  • 1
  • 1
CyrilleGuimezanes
  • 1,598
  • 1
  • 18
  • 33

2 Answers2

2

You are almost there.

Annotate each step in the object path with the time when the object would get there (should be length/speed).

Next take each segment of the path and assume that the target only moves along that segment and use the answer you found to figure out where it would intersect. There are 3 options.

  1. You hit the target somewhere inside the segment. This is the solution you are looking for.
  2. You would hit the target after the end of the segment. This means that you are too far away so you can't hit the target while it's traveling this particular segment. You need to try the next segment of the path.
  3. You would hit the target before the initial point of the segment. This means that the segment is relatively close and you should be able to hit the target anytime you want. You can decide this is a solution and add some delay to your shot so that the target is inside the segment, or try the previous segment.

Given the above I would start with the first segment of the target's path. If you can't hit the target there, try with the next until you find one where you can hit the target. This will be the first position where the target is in range so the shot should be without delay.

Sorin
  • 11,863
  • 22
  • 26
  • In the post I linked I didn't use the accepted answer (which is in my opignon not well explained...) is it possible to use your answer with http://stackoverflow.com/a/3487761/1319359 (another answer of my linked question)? – CyrilleGuimezanes Apr 30 '16 at 08:48
  • @CyrilleGuimezanes yes, that will do. Just double check the handling of corner cases (like, the projectile is too slow to reach the target, or how you do the comparisons when the solution is very close to the end of a segment). – Sorin May 02 '16 at 09:37
1

Algorithm 1: Adjust launch time

  1. Calculate the time it will take for the projectile if the target is moving in a straight line directly away from the sender.

  2. Calculate where the target will be when that amount of time has elapsed.

  3. Calculate the distance and time it will take the projectile to reach that point.

  4. Delay the launch appropriately.

Downside: If the target is moving straight towards the sender and the initial distance is great, then this algorithm could give long delays.

If the delay is too great, you may have to recalculate, and use an earlier strike time.

Algorithm 2: Adjust projectile speed

  1. Use the algorithm you linked to find the strike time.

  2. Calculate where the target will be at that time.

  3. Set the projectile speed so that it reaches the strike point at the same time as the target. The projectile speed should be constant as determined at the launch time.

Downside: If the target immediately turns towards or away from the sender the projectile may have a noticably different speed than normal.

I like algorithm 2 better. I think that players will appreciate that the projectile starts it trek immediately. If you use algorithm 1, the game may seem unresponsive. I don't think that the occasional slow/fast projectile will disturb the player much.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82