嗯,我有一个二维框冲突代码,基本上循环遍历列表中的每个块名为“块”,它检查我是否靠近两侧和什么。C++ 2D Box-Collision,是吗?
它工作得很好,除了块的底部。当我向底部跳跃时,我希望我的玩家只是“反弹”。它这样做,但它非常糟糕。这很难解释,所以我希望你们可以发现我的底部碰撞代码有什么问题。
这里就是整个事情(这是在一个循环中运行):
for(unsigned int i = 0; i<blocks.size(); i++){
Block &b = blocks.at(i);
if(!b.passable==true){
//Check if we are on the sides
if(y + height + vspeed >= b.worldY+2 && y + vspeed <= b.worldY+b.height)
{
//Right side
if(x + hspeed <= b.worldX+b.width-1 && x + hspeed > b.worldX+b.width + hspeed-2)
{
x = b.worldX + b.width; hspeed = 0;
}
//Left side
if(x + width + hspeed >= b.worldX +1 && x + width + hspeed <= b.worldX + hspeed + 2)
{
x = b.worldX - width; hspeed = 0;
}
}
//Check if we are on the top or the bottom
if(x + width + hspeed >= b.worldX+2 && x + hspeed <= b.worldX+b.width-2)
{
if(y + height + vspeed >= b.worldY && y + height + vspeed <= b.worldY + vspeed + 1 && jumpstate=="falling")
{
y = b.worldY - height; jumpstate.assign("ground"); vspeed = 0;
}
if(y + vspeed <= b.worldY + b.height && y + vspeed >= b.worldY + b.height + vspeed - 1 && jumpstate=="jumping")
{
y = b.worldY + b.height; jumpstate.assign("falling"); vspeed = 0;
}
}
}
}
[2D Box Collision - This is right?]的可能重复(http://stackoverflow.com/questions/6561341/2d-box-collision-is-this-right) –