After searching a large amount of sites, including this one, every solution i have came across seems to not work, or only work on the first object in the list.
The Problem
Slick2Ds Rectangle class offers many different methods for collision, one being the intersects(Rectangle box); method that correctly detects collisions when using this class in conjunction with javas built in ArrayList class,to an extent. The problem I'm having is that only the first object in the list is being detected and correctly collided upon.
The following code handles collisions:
public void move(float x, float y) {
if (blocks.size() > 0) {
for (int i = 0; i < blocks.size(); i++) {
Rectangle r = blocks.get(i);
Rectangle p = new Rectangle(xx + x, yy + y, 32, 32);
if (!r.intersects(p)) {
xp = xx;
yp = yy;
xx += x;
yy += y;
break;
} else {
xx = xp;
yy = yp;
xx += 0;
yy += 0;
System.out.println("Collide" + new Date());
break;
}
}
}else{
xp = xx;
yp = yy;
xx += x;
yy += y;
}
}
Key: xx = Player x; yy = Player y; xp = Players Last x Position; yp = Players Last y Position; r = Current Rectangle in the iteration list; p = Pre-calculated next position of the player
This basic codes job is to add movement by simulating a rectangle that will be in the next position the player is. if it does not collide then the player wont collide and we can move, but if it does collide then we don't move the player as the next location is not open.
But the code has a flaw as at some point during the iteration only the first box works, the others do detect collides but does not stop the box.
Correct Collision; logged in the console to confirm
Incorrect Collision; not logged in the console, even though its another instance of the same object
r.intersects(p)if statement break? - KarlArrayListis not important to the question or solution, any sort ofListwill act the same; indeed, anyIterablewill also do if you use the enhanced for loop likefor (final Rectangle r : blocks) { ...(and in Karl's answer) - Stephen P