我的游戏碰撞有问题。游戏是“基于平铺的”。当我放置一个应该阻止玩家走进它的瓷砖时,问题就出现了,它会在瓷砖附近产生一个无限的垂直和水平屏障。
这是我的代码:
double d = delta;
Point oldVerticalPoint = new Point(point.getX(), point.getY());
if (Keyboard.isKeyDown(Keyboard.KEY_UP))
point.setY((float) (point.getY() - (speed * d)));
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
point.setY((float) (point.getY() + (speed * d)));
for (int x = 1; x <= game.map.width; x++) {
for (int y = 1; y <= game.map.height; y++) {
Block block = game.map.getBlock(x, y);
if (block instanceof BlockWall) {
BlockWall b = (BlockWall) block;
Point blockPoint = Block.getStandardBlockCoords(x, y);
Point playerPoint = game.thePlayer.point;
if (playerPoint.getY() > blockPoint.getY() && playerPoint.getY() < <blockPoint.getY() + Block.LENGTH)
game.thePlayer.point = oldVerticalPoint;
}
}
}
Point oldHorizontalPoint = new Point(point.getX(), point.getY());;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
point.setX((float) (point.getX() - (speed * d)));
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
point.setX((float) (point.getX() + (speed * d)));
for (int x = 1; x <= game.map.width; x++) {
for (int y = 1; y <= game.map.height; y++) {
Block block = game.map.getBlock(x, y);
if (block instanceof BlockWall) {
BlockWall b = (BlockWall) block;
Point blockPoint = Block.getStandardBlockCoords(x, y);
Point playerPoint = game.thePlayer.point;
if (playerPoint.getX() > blockPoint.getX() && playerPoint.getX() < blockPoint.getX() + Block.LENGTH)
game.thePlayer.point = oldHorizontalPoint;
}
}
}
我不知道它为什么这样做。需要明确的一点是,为什么会有两次碰撞检测,所以如果玩家水平移动到墙壁上并且在诸如简单瓷砖之类的东西上垂直移动,玩家将不会完全停止移动。还使用getStandardBlockCoords,因为例如,如果块x和y是x = 1和y = 2,则意味着该块位于第二列和第一行。该方法只是将其转换为像素坐标。
我正在使用 LWJGL 和 Slick2D。帮助?