I’m having trouble doing collision detection. I’m able to walk trough blocks and I can’t find the problem.
This is the part of the code that checks collision:
AABB me = new AABB(position.x, position.y, position.z, position.x, position.y, position.z);
for (int x = 0; x < world.getXSize(); ++x) {
for (int y = 0; y < world.getYSize(); ++y) {
for (int z = 0; z < world.getZSize(); ++z) {
AABB block = new AABB(x, y, z, x + 1, y, z + 1);
if (me.intersects(block) || block.intersects(me)) {
return;
}
}
}
}
AABB class:
public class AABB {
public float x0;
public float y0;
public float z0;
public float x1;
public float y1;
public float z1;
public AABB(float x0, float y0, float z0, float x1, float y1, float z1) {
this.x0 = x0;
this.y0 = y0;
this.z0 = z0;
this.x1 = x1;
this.y1 = y1;
this.z1 = z1;
}
public boolean intersects(AABB c) {
if ((c.x1 <= this.x0) || (c.x0 >= this.x1)) {
return false;
}
if ((c.y1 <= this.y0) || (c.y0 >= this.y1)) {
return false;
}
if ((c.z1 <= this.z0) || (c.z0 >= this.z1)) {
return false;
}
return true;
}
}