So I have the code below that enables a block to follow the player. The block moves at a constant speed and rotates itself to the direction of the player to follow. However, the block only follows the player when it's above the player. When the block is below the player, it goes straight down instead of the direction of the player. I am wondering if there are any adjustments I could make in my algorithm to accomplish this task.
xMove = speed * Math.sin(angle);
yMove = speed * Math.cos(angle);
if(radarRange().intersects(Player.getPlayerData().getBounds())) {
double sx = x + width/2;
double sy = y + height/2;
double ex = Player.getPlayerData().getX() + Player.getPlayerData().getBounds().width/2;
double ey = Player.getPlayerData().getY() + Player.getPlayerData().getBounds().height/2;
double requiredAngle = Math.atan2(ex - sx, ey - sy);
if(angle < requiredAngle) {
turnTimer += System.currentTimeMillis() - lastTurnTimer;
lastTurnTimer = System.currentTimeMillis();
if (turnTimer < turnCooldown)
return;
angle += Math.PI/25;
turnTimer = 0;
} else if(angle > requiredAngle) {
turnTimer += System.currentTimeMillis() - lastTurnTimer;
lastTurnTimer = System.currentTimeMillis();
if (turnTimer < turnCooldown)
return;
angle -= Math.PI/25;
turnTimer = 0;
}
}