1
\$\begingroup\$

How could I efficiently calculate shoot direction for enemy AI in order for it to hit the player?

Things I know:

player position, player velocity, gravity, bullet spawn position, bullet initial speed(not direction, gravity is also applied for it)

Bullets are slow and don't hit instantly.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

I spent weeks on a very similar problem. With the help of DMGregory, I finally solved it!

First you find the spawn position for the bullet (wherever you want). Then you find the destination for the bullet (calculate where the player will be...let me know if you need help with this part).

Then you cheer DMGregory's name for his insanely awesome awesomeness. Read How can I launch a GameObject at a target if I am given everything except for its launch angle?, DMGregory's answer, and the answer linked in his answer.

I know you are in Java, but you can take a look at my C# version for reference. A simpler version is also my answer to my question linked in your question above. If you have any questions on moving this to Java, lemme know.

The math is crazy here. Good luck!

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm testing it for like 10 minutes and I think it's working fine. Thanks, bro! :) \$\endgroup\$ – Gintas_ Mar 7 '16 at 21:06
  • 1
    \$\begingroup\$ @Gintas_ did you convert straight from my version in that other question? If so, be aware of a bug with 0 gravity and diving by 0. \$\endgroup\$ – Evorlor Mar 8 '16 at 13:35
  • \$\begingroup\$ I used your C# version. Yeah, thankfully, I don't have 0 gravity. \$\endgroup\$ – Gintas_ Mar 8 '16 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.