如何与瓷砖地图进行适当的碰撞?
这是我目前的解决方案:
int topTileX = ((tileMap.xOffs - x) - 32 - 1) / Tile.TILE_SIZE;
int topTileY = ((tileMap.yOffs - y) - 32 - 1) / Tile.TILE_SIZE;
int bottomTileX = ((tileMap.xOffs - x) - 1) / Tile.TILE_SIZE;
int bottomTileY = ((tileMap.yOffs - y) - 1) / Tile.TILE_SIZE;
int leftTileX = ((tileMap.xOffs - x) - 32-1) / Tile.TILE_SIZE;
int leftTileY = ((tileMap.yOffs - y) - 32-1) / Tile.TILE_SIZE;
int rightTileX = ((tileMap.xOffs - x)-1) / Tile.TILE_SIZE;
int rightTileY = ((tileMap.yOffs - y)-1) / Tile.TILE_SIZE;
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
if (!tileMap.getLayer(0)[Math.abs(topTileY+1)][Math.abs(topTileX)].isBlocked()) {
tileMap.yOffs++;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
if (!tileMap.getLayer(0)[Math.abs(bottomTileY-1)][Math.abs(bottomTileX)].isBlocked()) {
tileMap.yOffs--;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
if (!tileMap.getLayer(0)[Math.abs(rightTileY)][Math.abs(rightTileX-1)].isBlocked()) {
tileMap.xOffs--;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
if (!tileMap.getLayer(0)[Math.abs(leftTileY)][Math.abs(leftTileX+1)].isBlocked()) {
tileMap.xOffs++;
}
}
但是有了这个,玩家会被困在角落里,有些瓷砖甚至无法工作。告诉我检查碰撞和处理碰撞的正确方法,而不会让游戏卡住等等。