Hello, I been working on my game for a good 1month now and i am having trouble with entity collision i managed to code it in and it works and everything but the entitys keep getting stuck and bouncing around any idea on a better way to do it?
http://s28.postimg.org/u8obiwam5/help.png
Current Code
Array<Enemie> enemies = MainGame.getEntityHandler().getEnemies();
for (int i = 0; i < enemies.size; i++) {
boolean xl = false;
boolean xs = false;
boolean yl = false;
boolean ys = false;
Enemie e = enemies.get(i);
if (e.equals(this))
continue;
if (getVelocity().x < 0) {
xs |= checkCollisionOnEnemie(e);
if (xs) {
getVelocity().x = 0;
setX(oldX+2);
}
} else if (getVelocity().x > 0) {
xl |= checkCollisionOnEnemie(e);
if (xl) {
getVelocity().x = 0;
setX(oldX-2);
}
}
if (getVelocity().y < 0) {
ys |= checkCollisionOnEnemie(e);
if (ys) {
getVelocity().y = 0;
setY(oldY+2);
}
} else if (getVelocity().y > 0) {
yl |= checkCollisionOnEnemie(e);
if (yl) {
getVelocity().y = 0;
setY(oldY-2);
}
}
private boolean checkCollisionOnEnemie(Enemie e) {
if (e.getBoundingRectangle().overlaps(getBoundingRectangle())) {
return true;
}
return false;
}