嗯,我有一個二維框衝突代碼,基本上循環遍歷列表中的每個塊名爲「塊」,它檢查我是否靠近兩側和什麼。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) –