im having trouble getting this rotation function down pat. i have a turret that should be rotating towards another moving object. it should find the shortest direction to rotate.
basically what ive done is converted action script rotation from using -1 -> -179 to 180 -> 359. but now im having issues when the object it is tracking passes over the zero degree point. the turrent then rotates around the other way.
here is the code. my mind is getting a little boggled. it is being passed the x difference between the objects, and the y difference
public function Move(xdiff:Number,ydiff:Number){
var currentRotation:Number;
rot = Math.round(Math.atan(ydiff/xdiff) * (180/Math.PI));
if(xdiff < 0){
//this.rotation = 180 + rot;
rot = rot + 180
}else{
//this.rotation = rot;
}
if(rot < 0 && rot > -90){
rot = 270 + 90 + rot;
}
trace("idealrotation: " + rot + "currentrotation: " + rotation);
currentRotation = rotation;
if(currentRotation < 0){
currentRotation += 360;
}
if(rot - currentRotation > 0){
if(Math.abs(rot-currentRotation) < 5){
rotation = rot;
}else{
rotation += rotationSpeed;
}
}else if(rot - currentRotation < 0){
if(Math.abs(rot-currentRotation) < 5){
rotation = rot;
}else{
rotation -= rotationSpeed;
}
}else{
rotation = rot;
}
//this.rotation = rot;
}