# Calculating missile trajectory around orbits before shooting

I'm building a game with Unity3D. It's a Gravity Wars clone. Both player and AI turrets shoot missiles at each other (giving an Angle and a Power variables), trying not to crash missiles on planets. But here's my question: how do I make AI calculate power and angle before shooting his missile, considering a planet's gravity too?

-

A system like this, with multiple bodies, is going to be chaotic. I don't think that you would be able to solve an equation for it in real-time. The best you can hope is to find a solution using a genetic algorithm;

1: produce a number (e.g.100) of random solutions (angle, power pairs).

2: simulate these solutions.

3: if any of these, end up hitting the target (or coming sufficiently close), Done! otherwise continue.

4: pick best 10 solutions (ones that end up closest to the target)

5: from these 10 solution, create 10 children for each, by randomly adjusting their angle and power.

6: now you have 100 new solutions, got back to step 2

You will need to limit the number of iterations, in case there is no solution to be found, or it is taking too long to search.

Even this approach is not guaranteed to find good solution because; 1. solution might not exist 2. in a chaotic system, small changes to a solution can have a huge impact on the result

-

How about making it realistic by not having them calculate, but starting with a guess and adjusting appropriately?

When I played Gravity Wars, this is what I did; start with a semi-random power, and adjust accordingly by an increment. Within a couple of shots, you get really close.

-

Given that the trajectory of the missle is based on the inputs of `Angle` and `Power`, you should be able to solve (get an exact answer) from an inverse equation.

The basic (psuedo) AI steps are as follows:

1. Pick a random `Power` level. It doesn't matter what the exact value is (to an extent), so long as it falls in a reasonable range.
2. Solve the (inverted from the actual pathing) equation to give the exact (well, as near as possible with doubles) `Angle`.
3. Pick some a (modifyable) random offset from the `Angle`, to adjust for the 'difficulty' of the AI.
4. Fire the missle as a player would, at the (random) `Power` and (calculated) `Angle`.

You could of course just have the AI fire at random levels for both inputs, which could produce some interesting results...

-
This method makes perfect sense when all you have is gravity straight down and a constant wind, but what do you use for an "equation to give the exact `Angle`" when you have multiple gravitational bodies scattered through your universe? Their pull depends on your current position. Some shots may not even be possible. – John McDonald Nov 25 '11 at 20:32
@John - If a shot is completely impossible (discounting a poor input power choice), there's a serious problem with your playfield. With these types of games, any one of the players should be replaceable with an AI. Granted, the equations get more complicated (and with multiple bodies, very quickly), but it should still be possible. – Clockwork-Muse Nov 25 '11 at 21:54
Fair enough. But do you know what you'd use for an equation? I think that's why this question was created. – John McDonald Nov 25 '11 at 22:04
John is right: what i'm looking for is that equation. – Onofrio Nov 25 '11 at 22:15
@Onofrio - Sorry, I don't have the relevant equations... and it looks like you need multi-variable calculus to do this 'properly'. – Clockwork-Muse Nov 25 '11 at 23:44

You should be able to simulate a shot, without drawing it.

Then you could say, simulate 10 shots, and then take the closest one of the 10.

-