I am creating a space game so gravity is not a factor but I wanted to calculate a weapon’s linear velocity when it’s fired against a moving target to ensure that the AI weapon leads the target with relative accuracy and ensuring the code is as simple as possible. With @pspeed’s help I solved the calculations and figured I would share the code for anyone who needs it.
Note: before we both get slammed about “accuracy”, we do realize that this is only a guess since it doesn’t take the prediction of where the ship will be at time ‘t’ into account. But this math is simpler.
readjust the targets position by predicting new position after time to target
result from last = adjusted aim vector for now better predicted position
now start from 1 again to improve accuracy further.
do this for around 2-5 loops based on weapon type and required accuracy. (more loops for stuff like sniper, for a minigun 1 might be enough with the inherent spread)
This can btw be improved witha heuristic to work for non linear weapons, like accelerating rockets, self targeting ammonution ect. basically just probe a few different shoot scenarios based on the heuristic and continue from the one with the best min distance between flightpath and target.