2d game : fire at a moving target by predicting intersection of projectile and unit

后端 未结 11 764
庸人自扰
庸人自扰 2020-11-28 22:26

Okay, this all takes place in a nice and simple 2D world... :)

Suppose I have a static object A at position Apos, and a linearly moving object B at Bpos with bVeloci

11条回答
  •  迷失自我
    2020-11-28 22:47

    First rotate the axes so that AB is vertical (by doing a rotation)

    Now, split the velocity vector of B into the x and y components (say Bx and By). You can use this to calculate the x and y components of the vector you need to shoot at.

    B --> Bx
    |
    |
    V
    
    By
    
    
    Vy
    ^
    |
    |
    A ---> Vx
    

    You need Vx = Bx and Sqrt(Vx*Vx + Vy*Vy) = Velocity of Ammo.

    This should give you the vector you need in the new system. Transform back to old system and you are done (by doing a rotation in the other direction).

提交回复
热议问题