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.
Rule and problem
Let x
, v
and a
are position, velocity, acceleration of the spaceship.
Let F
be the force of jet that apply to the ship.
I know mass m
of the spaceship, let's say m
=1.
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 :- x
=0 and v
=0.
AI can "accelerate" or "decelerate" the jet :-
F+=flexibility;
or
F-=flexibility;
AI can access current x
, v
and F
. AI can also cache it.
How should I program the AI?
My poor solution
Idea 1 : At last, x
should = 0.
Assume that 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 x
=0.
Idea 2 : At last, v
should = 0.
(current v) + a*t = 0
I mixed both ideas :-
if |x|>=thresholdX
--> use idea 1
if |x|~0
--> use idea 2
in between --> weight average of 2 ideas
Here, thresholdX
is another magic number.
I use a
from the equation to find appropriate F
. (F=ma
)
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).