Best way to do Entity Collision (SOLVED)

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;
	}

Look into “Minimum Translation Vector”, you can apply this to basic AABB collisions however it works best with SAT implementation.

What this does is allow you to find the intersection point, how far inside the objects are and gives you a vector that you can use to offset the 2 objects, separating them.

Create a bounding rectangle for each entity and check if the rectangles intersect.

You could go pixel perfect but that’s much slower. Where you check all the pixels on each entity to see if any overlap.

Depending on how efficient it runs you can use a tree structure to organize your entities if you really have to.

Yeah that was the way i went and the Entitys keep getting stuck within each other sometimes they don’t but most of the time they do

Check if they collide at the new position before you set them to the new position.

Yeah i have same thing is happening, any other ideas?

Yes! fixed it, I had to check the bounds from each side other wise the collision would keep them stuck

http://s21.postimg.org/bkrq1ra9j/helpfixed.png