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
Given that the trajectory of the missle is based on the inputs of
Power, you should be able to solve (get an exact answer) from an inverse equation.
The basic (psuedo) AI steps are as follows:
- Pick a random
Powerlevel. It doesn't matter what the exact value is (to an extent), so long as it falls in a reasonable range.
- Solve the (inverted from the actual pathing) equation to give the exact (well, as near as possible with doubles)
- Pick some a (modifyable) random offset from the
Angle, to adjust for the 'difficulty' of the AI.
- Fire the missle as a player would, at the (random)
You could of course just have the AI fire at random levels for both inputs, which could produce some interesting results...
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.