When I move, I grab the tile in front of the player, but when the player is colliding between two tiles, I grab the wrong tile due to rounding error. I made a video explaining what I mean.
As you can see, when the player is moving down on the edge of a tile, they pass through. This is how I find the tile:
Tile t = world.tiles[(int) ((int) getX() / 32 + x)][(int) ((int) getY() / 32 + y)];`
This is my collision detection code:public static boolean isColliding(AABB a, AABB b) {
if (Math.abs(a.pos.getX() - b.pos.getX()) < a.size + b.size) {
if (Math.abs(a.pos.getY() - b.pos.getY()) < a.size + b.size) {
return true;
}
}
return false;
}
The pos variable in the AABB class is the center of the entity and the size is half the size of the player.
How can I fix this so it finds the correct tile?
Edit: Ok, I changed my move method to get the tiles around the player, but now the player only collides if he is colliding in the middle of four solid tiles! I don't understand!
private void move(int x, int y, float sx, float sy) {
Vector2f tempPos = new Vector2f((pos.getX() + (pos.getX() + 32)) / 2, (pos.getY() + (pos.getY() + 32)) / 2);
box.update(new Vector2f(tempPos.getX() + sx, tempPos.getY() + sy), 16);
if (getX() / 32 + x > 0 && getX() / 32 + x < world.mx && getY() / 32 + y > 0 && getY() / 32 + y < world.my) {
for (int xx = 0; xx <= 1; xx++) {
for (int yy = 0; yy <= 1; yy++) {
if (world.getTile((int) (getX() / 32) + xx, (int) (getY() / 32) + yy) != null) {
Tile t = world.getTile((int) (getX() / 32) + xx, (int) (getY() / 32) + yy);
t.color = true;
if (t.type != 0 && !Collision.isColliding(box, t.getBox())) {
setPos(getX() + sx, getY() + sy);
} else if (t.type == 0) {
setPos(getX() + sx, getY() + sy);
}
} else {
setPos(getX() + sx, getY() + sy);
}
}
}
} else {
setPos(getX() + sx, getY() + sy);
}
glPushMatrix();
glTranslatef(getX(), getY(), 0);
glPopMatrix();
}
The type in Tile is just for checking to see if the tile is air. As you can see, I have two for loops that loop through the four tiles above, below and to the right and left of the player, and see if he collides with any of them. I have no idea why this isn't working, and I've tried to figure out how to do this for a week now. Can anyone show me a link or code to get the tiles around a player and see if he collides with them?