a PD Controller
or more generically a PID Controller
generates a control output (for example how fast to turn) based on P proportional, I integral, and D derivative inputs.
in your case 'P' would be the difference in degrees between current direction and target direction
'D' would be the deriviative of 'P' -how fast that angle difference changes over time
and 'I' we shall ignore - this is more useful for stuff like Air Conditioners
psuedocode PID Controller:
class PID{
float P,I,D;
void init(float p,float i,float d){P=p;D=d,I=i;}
float controldecision(float error){
static float deriv=0;
static float lasterror=0;
static float integral=0;
deriv=error-lasterror;
lasterror=error;
integral+=error;
return (P*error)+(I*integral)+(D*deriv);
}
}
That code uses Static variables, so you will need a new instance for each object being controlled.
the idea is that each gameloop (better have a fixed timestep) you will call controldecision(), and pass the signed difference in degrees between your direciton and target as an argument. it will return a float, which you will use as your rotation force.
The trick of course, is initializing the Controller with Good values for P,I,and D.
it needs to be 'tuned' to work with whatever you are trying to control.
In your case, i'd leave 'I' as 0.
Start out with P and D as something small, like .5 for now
experiment with them, which one is larger or smaller, etc, till it starts acting properly.
if it occilates too much (back and fourth motion) try increasing D
if it doesnt move fast enough, try increasing P