I am having some problems with my method for turning my entity to face another Entity. The angle calculation works perfectly, however the entity will rock back and forth flying away from the target if it is directly below it. I think it is my turning to desired rotation method…
MoveOrder mo = (MoveOrder) baseEntity.orders.get(0);
if(baseEntity.x < mo.x+16 && baseEntity.x > mo.x-16 && baseEntity.y < mo.y+16 && baseEntity.y > mo.y-16)baseEntity.orders.remove(0);
double xd = mo.x - baseEntity.x;
double yd = mo.y - baseEntity.y;
double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;
while(rotation > 360)rotation -= 360;
while(rotation < 0)rotation += 360;
if(baseEntity.rot > rotation+turnSpeed){
baseEntity.rot -= turnSpeed;
}else if(baseEntity.rot < rotation - turnSpeed){
baseEntity.rot += turnSpeed;
}else{
baseEntity.rot = rotation;
}
Could anyone help me sort out how the ships turn to the desired rotation?