【发布时间】:2018-01-28 21:26:55
【问题描述】:
所以这个问题之前可能已经被问过几次了,但我似乎无法找到让碰撞检测正常工作的方法。
我正在尝试制作 HTML 画布/Javascript 平台游戏。如果你想看看它的作用,它在 codepen 上:https://codepen.io/Arthurvanhoorebeke/pen/zpaZYM
我正在做的是检查我的播放器的角落是哪些瓷砖/“盒子”。这意味着我可以检测到播放器上方和下方的方块,但侧面检测非常糟糕。
我知道检查底部碰撞是什么弄乱了侧向检测,但我似乎无法修复它。 这就是我现在得到的:
function tileCheck(world){
var player_topy = Math.ceil(player1.pos[1] / world.squareHeight) || 0;
var player_bottomy = Math.ceil((player1.pos[1]+player1.height)/world.squareHeight) || 0;
var player_leftx = Math.floor(player1.pos[0]/world.squareWidth) || 0;
var player_rightx = Math.floor((player1.pos[0]+player1.width)/world.squareWidth) || 0;
var topRightBlock = getSquare(world1,player_rightx,player_topy-1);
var topLeftBlock = getSquare(world1,player_leftx,player_topy-1);
var bottomRightBlock = getSquare(world1,player_rightx,player_bottomy-1);
var bottomLeftBlock = getSquare(world1,player_leftx,player_bottomy-1);
//checks if blocks are solid
var topRightSol = world.legend[topRightBlock].solid;
var topLeftSol = world.legend[topLeftBlock].solid;
var bottomRightSol = world.legend[bottomRightBlock].solid;
var bottomLeftSol = world.legend[bottomLeftBlock].solid;
//This kinda messes up my 'sideways collision detection'.
if((bottomRightSol && !topRightSol) || (bottomLeftSol && !topLeftSol)){
player1.pos[1] -= (player1.pos[1]+player1.height) - (player_bottomy-1)*world.squareHeight;
player1.vel[1] = 0;
player1.grounded = true;
}
else{
player1.grounded = false;
}
if((topLeftSol && !bottomLeftSol) || (topRightSol && !bottomRightSol)){
player1.pos[1] += player_topy*world.squareHeight - player1.pos[1];
player1.vel[1] = 0;
}
//This doesn't work properly.
if(topLeftSol && bottomLeftSol){
player1.vel[0] = 0;
player1.wallLeft = true;
player1.pos[0] += (player_leftx+1)*world.squareWidth - player1.pos[0];
}
else{player1.wallLeft = false}
if(topRightSol && bottomRightSol){
player1.vel[0] = 0;
player1.wallRight = true;
player1.pos[0] -= (player1.pos[0]+player1.width) - (player_rightx)*world.squareWidth;
}
else{player1.wallRight = false}
}
我知道当玩家方块只有一个角落撞到其他东西时,侧向检测是行不通的。问题是当玩家击中一个方块时,它的垂直位置会被底部碰撞检查器改变(至少我认为这是问题所在。)。
如果您想查看确切的问题,请尝试打开 codepen 链接并跳一下...
是否有解决此问题的方法,还是我需要重新考虑我的整个功能?
【问题讨论】:
标签: javascript html