所以,我第一次去画布,我试图让一个简单的角色在一个房间里走动,那里有可碰撞的物体,这些物体都在工作。我试图写下应该发生的事情的逻辑,如果我站在对象内但它会把我带到画布中的随机位置。
这是我试图使用的内容:
if ((collisionPos[1] + collisionSize[1]) <= (player.pos[1] + player.sprite.size[1])) {
player.pos[1] = (collisionPos[1] + collisionSize[1]);
} else if ((player.pos[1] + player.sprite.size[1]) > collisionPos[1]) {
player.pos[1] = (collisionPos[1] - player.sprite.size[1]);
}
if ((player.pos[0] + player.sprite.size[0]) >= (collisionPos[0] + collisionSize[0])) {
player.pos[0] = (collisionPos[0] + collisionSize[0]);
} else if (collisionPos[0] < (player.pos[0] + player.sprite.size[0])) {
Player.pos[0] = (collisionPos[0] - player.sprite.size[0]);
}
collisionPos
是碰撞位置x
的{{1}}和y
的数组,(x, y)
是对象大小collisionSize
的数组1}}和(x, y)
是玩家。我无法看到我做错了什么。
如果它有助于存在的冲突 { pos:[0,0], 尺寸:[160,48] }, { pos:[160,0], 尺寸:[200,36] }, { pos:[360,0], 尺寸:[50,400] }, { pos:[240,48], 尺寸:[50,50] }, { pos:[172,120], 尺寸:[20,60] }
和player.sprite.size = [32,48]
答案 0 :(得分:0)
我想将此作为评论发布,但它太长了所以:
if ((collisionPos[1] + collisionSize[1]) <= (player.pos[1] + player.sprite.size[1])) {
// Collision bottom edge <= Player's bottom edge (<-- seems wrong I think no size should be inlcuded here.)
player.pos[1] = (collisionPos[1] + collisionSize[1]);
// player's top moves to botom edge of collision
} else if ((player.pos[1] + player.sprite.size[1]) > collisionPos[1]) {
// player's bottom edge > collision top edge
player.pos[1] = (collisionPos[1] - player.sprite.size[1]);
// player's top moves to player's top edge minus player size
}
if ((player.pos[0] + player.sprite.size[0]) >= (collisionPos[0] + collisionSize[0])) {
// player's right edge >= collision's right edge (<-- seems wrong I think no size should be inlcuded here.)
player.pos[0] = (collisionPos[0] + collisionSize[0]);
// player's left edge goes to collision's right edge
} else if (collisionPos[0] < (player.pos[0] + player.sprite.size[0])) {
// collision left edge < player's right edge
player.pos[0] = (collisionPos[0] - player.sprite.size[0]);
// player's left edge moves to collision's left edge minus player's size
}
我认为添加了一些玩家的精灵大小可能会导致一些奇怪的问题?
另外,更重要的是,你如何进行这项检查?因为如果你不小心独立地执行检查,玩家可能会在碰撞点旁边并且仍然位于“上方”位置。它或它下面。你应该同时检查x和y。基本上所有的碰撞盒都可以通过这种方式具有无限的宽度/高度。我想。