我正在开发平台游戏,我遇到了一些碰撞检测问题。游戏设置有许多彼此相邻或间隔开的区块,因此玩家从一个区块跳到下一个区块。现在玩家可以跳到障碍物的顶部,跑到障碍物的一侧,如果跳到障碍物的底部则停止。我现在遇到的问题是,如果我进入一个罕见的情况,即玩家落在区块的某个部分之上,游戏会将其识别为其旁边的区块和玩家的区块一侧穿过街区。因此,如果我跳到块[3]的最边缘并且块[4]就在它旁边,游戏会识别出玩家已经跳到了块的一侧[4]。知道如何解决这个问题吗?下面是我的碰撞检测的代码。
function checkBlockCollisions() {
// If a player collides with a block, their motion should be stopped.
for(var i = 0; i < blocks.length; i ++) {
var angle = blocks[i].angleTo(player);
var thisTop = blocks[i].y - blocks[i].height / 2;
var thisBottom = blocks[i].y + blocks[i].height / 2;
var playerTop = player.y - player.height / 2;
var playerBottom = player.y + player.height / 2;
if(blocks[i].collidesWith(player)) {
if((angle >= 55 && angle <= 360) && player.falling) {
// If the player is above the block, then they should stop falling.
player.falling = false;
player.onBlock = true;
player.setDY(0);
// Make sure the player is not inside the block.
player.setPosition(player.x, thisTop - player.height / 2);
}
//top of block is lower than the middle of the player to the top and bottom of block is higher than the middle of the player to the bottom
if(thisTop > (player.y - player.height / 2) && thisBottom < (player.y + (player.height / 2 ))) {
// If the player runs into the side of a block, only stop the sideways motion.
player.setPosition(player.x - player.dx, player.y);
player.setDX(0);
} // end if
else if(player.falling) {
// If the player is below the block, then they should keep falling but stop accelerating upwards and sideways.
player.setDY(0);
player.setDX(0);
player.setPosition(player.x, thisBottom + player.height / 2 + 1);
} // end if
} // end if
} // end for
} // end checkBlockCollisions
答案 0 :(得分:0)
查看我的引擎,它是一个完全用于碰撞的引擎,使用起来非常简单:https://github.com/Murplyx/AAE---Axis-Aligned-Engine