Collision code:
public void handleCollisions(Game game) {
if (!blockId.equals("air")) {
if (game.player.getBounds().intersects(getBounds())) {
if (game.player.playerY + Player.PLAYERHEIGHT > blockY) {
if (game.player.playerY + Player.PLAYERHEIGHT < blockY + (BLOCKSIZE / 2)) {
game.player.playerY = blockY - Player.PLAYERHEIGHT;
game.player.playerYV = 0;
game.player.jump = true;
} else {
if (game.player.playerX < blockX + paintWidth) {
game.player.playerX = blockX + paintWidth;
}
if (game.player.playerX > blockX - Player.PLAYERWIDTH) {
game.player.playerX = blockX - Player.PLAYERWIDTH;
}
}
}
}
}
}
Mainly looking at:
if (game.player.playerX < blockX + paintWidth) {
game.player.playerX = blockX + paintWidth;
}
if (game.player.playerX > blockX - Player.PLAYERWIDTH) {
game.player.playerX = blockX - Player.PLAYERWIDTH;
}
paintWidth is the width of the block.
When the player walks right into the left side of a block, it works fine; the player stops in front of it. However, when the player walks left into the right side of a block, it teleports the player to the left side of the block. But to add even more to the confusion, if I put an else in front of
if (game.player.playerX > blockX - Player.PLAYERWIDTH)
the teleporting side will switch to the other side. (walking left into a block is fine, walking right will teleport)
My math seems to be fine, it looks like it’s a quirk in Java…? Looking for some guidance on this. I can post more of my code if necessary, but I have 9 classes that all link into each other and I’d prefer not to post it all if possible.
(inb4 “use a game engine n00b”)