How to predict the player movement with vector math in a shmup?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leandro

Greetings from Brazil. I’m developing a 2D shoot’ em up game In this game genre, is quite common to have enemies that shoot aiming at the player’s current position. Since in my code all positions and speed are represented with 2D vectors, the formula to calculate the desired enemy shot direction is quite simple: (playerx,playery)-(enemyx,enemyy).

However, I want to try a different twist. Instead of having enemies that aim at the player CURRENT position, I want to create enemies that instead aim at the player FUTURE position. Both player speed and enemy bullet speed are constant. Is there any vector formula that I can use to calculate the enemy bullet direction, so it’ll intercept the moving player? Thanks in advance!

enter image description here

Whether or not there is a formula, and how complex it is depends very much on what you Expect of the players behavior.

Are you doing a top down shooter and expecting the player to continue in straight lines? In that case…

(Vector_to_player + Player.Velocity* distance_to_player/BulletVelocity ).normalized()*bulletVelocity will give a reasonable approximation.

If you are in a 2d platformer with gravity involved, things get more complicated.

nineGradens | 2019-07-19 21:04

:bust_in_silhouette: Reply From: Dooowy.

You would need to learn about a math library specifically for this.