how to seperate the closest x and y and go to that direction
my code
int targetX = x;
int targetY = y;
public int speed = 5;
public int dirY, dirX;
public void moveLogic() {
getFood1();
if (targetX != x || targetY != y) {
int pathX = targetX - x;
int pathY = targetY - y;
double distance = Math.sqrt(pathX * pathX + pathY * pathY);
double directionX = pathX / distance;
double directionY = pathY / distance;
double movementX = directionX * speed;
double movementY = directionY * speed;
dirX = (int) movementX;
dirY = (int) movementY;
}
move();
}
public void move() {
x += dirX;
y -= dirY;
}
public void getFood1() {
if (targetX == x && targetY == y) {
int[] foodX = new int[game.food.size()];
int[] foodY = new int[game.food.size()];
for (int i = 0; i < game.food.size(); i++) {
foodX[i] = game.food.get(i).x;
foodY[i] = game.food.get(i).y;
}
for (int i = 0; i < foodX.length; i++) {
for (int j = 0; j < foodY.length; j++) {
if (game.food.get(i).x - x < 40) {
targetX = game.food.get(i).x;
if (game.food.get(i).y - y < 40) {
targetY = game.food.get(i).y;
}
}
}
}
}
}
and it is not working please help