I am creating a 2D game in which two space ships, orbiting around a planet under the influence of gravity, fire projectiles at each other, which are also under the influence of gravity.
I'm creating an AI for this game that needs to compute the trajectory its bullet should have in order for the bullet orbit the planet and eventually hit its enemy.
If we assume that the velocity of the bullet and space ship are constant, the problem is very simple, there are tons of pages out there on how to lead a target, and I have implemented that target-leading algorithm in my game, but the bullet moves slow enough that by the time it reaches where the enemy space ship would have been, both the bullet and spaceships' velocity have changed due to gravity.
My first idea was that the ship and bullet would be moving in an ellipse. I decided to look up the parametric equation for an ellipse, setting the X and Y functions equal to themselves, where the left side has the constants for the bullet's ellipse, and the right side has constants for the enemy's ellipse, and solving each equation for T, but I got lost there, and that won't cover parabolas and hyperbolas if the bullet or enemy reaches escape velocity.
So, my question: Assuming the planet is centered at the origin and doesn't move, given my spaceship's position/velocity, the enemy spaceship's position/velocity, the mass of the bullet, and the speed that the bullet will be launched from my ship (not including the velocity of the ship itself), how do I calculate a normalized vector representing the direction my ship should fire so that it hits the enemy?
It's ok if there are certain situations where it's impossible to hit the enemy - in those situations my ship can either not fire, or revert to the "constant velocity" formula.
I'm sure there will be multiple (potentially infinite) solutions, i'm interested in the solution that will occur soonest.
It's ok to ignore the possibility that the bullet hits the planet when computing which direction to fire in - once the direction is computed, i just need a way to check if the bullet's trajectory hits the planet before it hits the enemy. If it does, my ship just won't fire. If it's very simple to find multiple solutions that's a possibility, but it's not at all a priority.