http://codepen.io/AlexKM/pen/JGEMjZ
turnTo: function (X, Y) {
var angleDiff = 0,
targetA = this.angleToPoint({
x: X,
y: Y
}),
dronA = this.angle;
/// If the angle difference can't be reached in a frame
if (Math.abs(targetA - dronA) > this.turnSpeed * 1.25) {
this.workingAngle = true;
angleDiff = targetA - dronA;
while (angleDiff < 0)
angleDiff += 2 * Math.PI;
while (angleDiff > 2 * Math.PI)
angleDiff -= 2 * Math.PI;
if (angleDiff >= Math.PI) {
this.turningLeft = true;
} else {
this.turningRight = true;
}
} else /// if the diff is negligible, reach it to save CPU
this.angle = targetA;
},
Update function snippet dealing with actual turning:
// apply turning
if (this.turningLeft) {
this.angle -= this.turnSpeed;
}
else if (this.turningRight) {
this.angle += this.turnSpeed;
}
Red dot - drone facing
Orange dots - signal if the drone is turning left or right
Cyan dot - signal if the drone is recalculating angles/doing trigonometry
The code DOES contain a part which helps it smooth out, by basically simply setting to the mouse's angle if it can be reached within a frame from the testDrone.turnSpeed variable.
About half the time, it turns and works smoothly. The other half, it goes jittery, alternatively turns left and right and continuously calculates trig.
What could be the reason for this?