Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 2D框碰撞,对不对?_C++_2d_Collision - Fatal编程技术网

C++ 2D框碰撞,对不对?

C++ 2D框碰撞,对不对?,c++,2d,collision,C++,2d,Collision,我有一个2D方块碰撞代码,它基本上循环遍历一个叫做方块的列表中的每个方块,它检查我是否靠近侧面等等 它的工作非常好,除了底部的块。当我跳到底部时,我希望我的球员能简单地弹起。它可以做到这一点,但它非常容易出错。这很难解释,所以我希望你们能找出我的底部碰撞代码的错误 以下是在循环中运行的整个过程: for(unsigned int i = 0; i<blocks.size(); i++){ Block &b = blocks.at(i); if(!b.passable==true){

我有一个2D方块碰撞代码,它基本上循环遍历一个叫做方块的列表中的每个方块,它检查我是否靠近侧面等等

它的工作非常好,除了底部的块。当我跳到底部时,我希望我的球员能简单地弹起。它可以做到这一点,但它非常容易出错。这很难解释,所以我希望你们能找出我的底部碰撞代码的错误

以下是在循环中运行的整个过程:

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;
        }
     }
  }    
}

我不确定你能不能让它像这样工作。我将解释一个解决方案,我想到一些碰撞检测和反弹,工作顺利。记录上次检查碰撞和调整位置的时间间隔。如果Xplayer+Vplayer*deltaT>Xtarget,如果播放器将与目标重叠,则计算其接触目标的实际时间deltaTtouch=Xtarget Xplayer/Vplayer。现在将播放器弹回Xplayer=Xtarget Vplayer*deltaT deltaTtouch。当你向前、向后、向上、向下移动时,你必须弄清楚所有的情况。
LE:你也可以实现重力,这涉及到求解一个二次方程来找出deltaTtouch。

我可以看到一些小细节:

1在这一行中,检查旧位置时的hspeed是多余的,为了清晰起见,最好将其拆下

if(x + hspeed <= b.worldX+b.width-1  && x + hspeed > b.worldX+b.width + hspeed-2)
变成:

if(x + hspeed <= b.worldX + b.width - 1  && x > b.worldX + b.width - 2)
其他类似的线路也是如此

2,-2和-1的小偏移量有点让人困惑,我想你是在尝试实现一个小的缓冲区,这样就需要少量的重叠。特别是在这一行中,您使用了<,而不是
if(x + hspeed <= b.worldX+b.width-1  && x + hspeed > b.worldX+b.width + hspeed-2)
if(x + hspeed <= b.worldX + b.width - 1  && x >= b.worldX + b.width - 1)
if(y + height + vspeed >= b.worldY && y + height + vspeed <= b.worldY + vspeed + 1 && jumpstate=="falling")
if(y + height + vspeed >= b.worldY + 1 && y + height <= b.worldY + 1 && jumpstate=="falling")
if(y + vspeed <= b.worldY + b.height && y + vspeed >= b.worldY + b.height + vspeed - 1 && jumpstate=="jumping")
if(y + vspeed <= b.worldY + b.height - 1 && y >= b.worldY + b.height - 1 && jumpstate=="jumping")
if(y + height + vspeed >= b.worldY+2 && y + vspeed <= b.worldY+b.height)