Quote Originally Posted by de_jean_7777 View Post
Seems interesting. Though the math on the wiki page does not make it seem simple.
Yeah, but don't be fooled by the sigmas and other fancy math symbols, there is a pseudo-code snippet that is probably a lot simpler to understand (for non-mathematician), and if you restrict yourself to the PI variant (drop the "derivative" and its use in "output" line), it becomes very trivial.

The pseudo-code doesn't contain it, but in practice you'll want to either clamp the integral term or dissipate it over time (dissipation works well IME for game AI purposes), and assuming your game loop already has so kind of fixed timesteps and you compute the output only once per frame, you can use something like that:

Code:
function TControllerPI.Output(setPoint, feedBack : Float) : Float;
var
   error : Float;
begin
   error := setPoint - feedBack;
   FIntegral := FIntegral * FKdissipation + error;
   Result := FKp * error + FKi * FIntegral;
end;
setPoint is the value you desire (an angle, a speed, a position...)
feedBack is the current value (or delayed value) in the game/simulation
FKp & Fki are proportional and integral gains (to be tuned)
FKdissipation is the integral dissipation gain (between 0 and 1, start from 0.9 and tune up or down)

And output is the command to send to the simulation (steering, pressure on accelerator pedal, etc.).
If things go in the wrong direction, just negate the output or your gains.

Alternatively you can also use

Code:
Result := FKp * (error + FKi * FIntegral);
Some people find that variant easier to tune.

For tuning, the wikipedia article gives good info in http://en.wikipedia.org/wiki/PID_con...#Manual_tuning and http://en.wikipedia.org/wiki/PID_con...Nichols_method

Though for games AI, you don't need very good tuning (or it looks robotic)

You can cascade PID controllers, or have PID controllers feed into other PID controllers gains. That allows them to handle more complex control situations (like when there is a lot of inertia), or can be used to simulate an AI that is getting "angry" over time (by ramping up the gains, you can get jerky or twitchy-looking movements).