So I have this code before which is obviously very familiar. It currently moves all the zombies that I add to an arraylist towards my player, but if the zombie is positive of the player (to the right) then the zombie moves faster than it does on the left. Any ideas how this is fixed?
for (Zombie zombie : zombies) {
zombie.distX = Game.player.x - zombie.x;
zombie.distY = Game.player.y - zombie.y;
zombie.angle = Math.atan2(zombie.distY, zombie.distX);
zombie.x += Math.cos(zombie.angle) * zombie.speed;
zombie.y += Math.sin(zombie.angle) * zombie.speed;
}
EDIT: I made these changes which normalized it and now it works! Thanks!
zombie.angle = Math.sqrt(Math.pow(zombie.distX, 2) + Math.pow(zombie.distY, 2));
zombie.distX /= zombie.angle;
zombie.distY /= zombie.angle;
zombie.x += zombie.distX * 2;
zombie.y += zombie.distY * 2;