Вы используете позиции, чтобы определить, идете ли вы сверху или нет.
рассмотрим следующую таблицу:
\ | / Assume enemy is at the center.
\ y | y / Assume each angle is 45°
\ | / Marked x or y is what you will reverse
x \ | / X
_____\|/_____ An important feature of this is that
/|\ the Y marked areas will have a slope
x / | \ X through the origin where
/ | \ abs(slope) > 1
/ y | y \ And the X will have the remainder
/ | \
Я бы использовал что-то вроде этого:
// split these out just for code clarity hilarity
Rectangle me = new Rectangle((int) position.x, (int) position.y, size, size);
Rectangle them = new Rectangle((int) enemy.x, (int) enemy.y, enemy.width, enemy.height);
if(me.intersects(them)){
enemy.hp--;
// find the relative location
double relx = enemy.x - position.x;
double rely = enemy.y - position.y;
// find slope of line between the two of you
double slope = relx / rely;
// if abs(slope) > 1, you're in y territory, otherwise, x territory
if(Math.abs(slope) > 1.0) {
velo.y = -velo.y;
}
else {
velo.x = -velo.x;
}
}