I have to code AI to control many propulsion jets for a spaceship in a game.
For simplicity :-
Let the space be 1D.
Spaceship is a point and there is only 1 jet.
a are position, velocity, acceleration of the spaceship.
F be the force of jet that apply to the ship.
I know mass
m of the spaceship, let's say
Here is a summary :-
acceleration = F/m;
v = vOld + acceleration*timestep;
x = xOld + v*timestep;
The objective is to land the ship on a certain position with 0 velocity :-
AI can "accelerate" or "decelerate" the jet :-
AI can access current
F. AI can also cache it.
How should I program the AI?
Idea 1 : At last,
x should = 0.
a is constant :-
(current x) + (current v) * t + 1/2 * a * t * t = 0
t is a magic number - how much time its require to make the spaceship's
Idea 2 : At last,
v should = 0.
(current v) + a*t = 0
I mixed both ideas :-
|x|>=thresholdX --> use idea 1
|x|~0 --> use idea 2
in between --> weight average of 2 ideas
thresholdX is another magic number.
a from the equation to find appropriate
Here is a result :-
The graph is noisy because the mass is approximated by another AI, and there are some small random external forces.
If anybody asks, I can post my C++ code (~100 lines).
Firstly - Are you planning on landing on a body (which has mass), or just coming to a stop at some arbitray point in space? Your question says "land", so I'm assuming the former, in which case you need to factor gravity in as well. Which should be easy enough to do:
F_actual = F_engine - F_gravity.
Secondly - How would you do this in real life? Real-life pilots want to "establish" their aircraft on a "glide slope" (well before reaching the runway), with the aircraft "trimmed" so that in ideal conditions (no wind, etc) the plane could land itself with no control inputs (I'm simplifying a bit, and ignoring flare etc.)
For a rocket, I would probably want to get myself into a situation such that at some safe height above the ground, my descent rate is such that with the engine running at some constant power, the rocket would settle to the ground by itself, with no extra input from me except killing the engine at the point of touchdown. (Actually, I would hope that the flight system allowed me to arm an auto-kill on touchdown.)
To see how this would work, just run the problem in reverse. Starting at
x=0, v=0, with
a=some constant and reasonable acceleration that the engine can produce, plot the x and v over time as the rocket ascends. Obviously,
v=at (a line) and x is a summation of those values (a parabola).
That parabola is your "glide slope". Now rather than trying to get
v=0 at the same time (without x ever becoming negative), your problem has become "How do I hit the glide slope at a safe height?". So your logic would be something like:
xthat allows a constant
ato produce a gentle touch-down with no additional control inputs.
Hmmmm - even after editing, this post is quite long. I think I should stop right about..... now.
The first step is to implement the game with manual control. Build a simulation with physics and make some buttons for the pilot. If manual landing of the spaceship works than its time for the first AI-prototype. This is commonly implemented as a one-armed bandit, that means a random generator calculates the acceleration. The user has no other possibility than watch and hope. The next AI prototype which will also be implemented by hand uses a pseudorandom-generator. That means, the burst is in a certain range but remains randomly. The question is not, if the AI is able to land, the question is: need the AI 10 trials or 100 trials. The best practice for reducing failure-rate is to use rules like in the OP. These heuristics don't work in every situation they can only improve the pseudo-random-generator. It is a little bit like procedural generation.