物理和碰撞检测的意外行为

时间:2017-12-10 15:24:29

标签: c++ if-statement logic collision-detection physics

我已经解决了这个问题好几个月了,我目前的解决方案仍然不起作用。我正在研究方形棋子和方形瓦片矢量之间的简单边界框碰撞。

在当前的方法中,玩家将在底部,左侧和右侧正确碰撞但不在顶部。我不懂为什么。

这是物理函数,它处理运动:

void loop(Pawn *pawn) {

    int currentFPS = fps->getFPS();

    //friction
    if (pawn->velX > 0.0) {
        pawn->velX *= 0.91f;
    }
    if (pawn->velY > 0) {
        pawn->velY *= 0.91f;
    }
    if (pawn->velX < 0) {
        pawn->velX *= 0.91f;
    }
    if (pawn->velY < 0) {
        pawn->velY *= 0.91f;
    }

    //gravity
    pawn->velY += gravity;

    //motion
    pawn->posY += pawn->velY; //move along the horizontal axis
    pawn->posX += pawn->velX; //move along the vertical axis

    //handle collision
    for (int i = 0; i < vectorSize; i++) {
        collide(pawn, getLevelCollision[i]);
    }

}

这是碰撞函数,它检查碰撞,然后根据哪个边与碰撞矩形相交来修正pawn位置:

void collide(Pawn *pawn, SDL_Rect rect2) {

    bool collide = false;

    //create positions of rect1
    float rect1Top = pawn->posY;
    float rect1Bottom = rect1Top + pawn->position.h;
    float rect1Left = pawn->posX;
    float rect1Right = rect1Left + pawn->position.w;

    //create position of rect2
    float rect2Top = rect2.y;
    float rect2Bottom = rect2.y + rect2.h;
    float rect2Left = rect2.x;
    float rect2Right = rect2.x + rect2.w;



    //if the bottom edge of rect1 intersects rect2 vertically
    if (rect1Bottom > rect2Top && rect1Bottom < rect2Bottom) {
        //if rect1 and rect2 are on top of each other
        if (rect1Right > rect2Left && rect1Right < rect2Right || rect1Left < rect2Right && rect1Left > rect2Left) {
            pawn->posY = rect2Top - pawn->position.h;

            rect1Top = pawn->posY;
            rect1Top = pawn->position.h;
        }
    }

    //if the left edge of rect1 intersects rect2 horizontally
    else if (rect1Left < rect2Right && rect1Left > rect2Left) {
        //if rect1 and rect2 are on top of each other
        if (rect1Top < rect2Bottom && rect1Top > rect2Top || rect1Bottom > rect2Top && rect1Bottom < rect2Bottom) {
            pawn->posX = rect2Right;

            rect1Left = pawn->posX;
            rect1Right = rect1Left + pawn->position.w;
        }
    }

    //if the right edge of rect1 intersects rect2 horizontally
    else if (rect1Right > rect2Left && rect1Right < rect2Right) {
        //if rect1 and rect2 are on top of each other
        if (rect1Top < rect2Bottom && rect1Top > rect2Top || rect1Bottom > rect2Top && rect1Bottom < rect2Bottom) {
            pawn->posX = rect2Left - pawn->position.w;

            rect1Left = pawn->posX;
            rect1Right = rect1Left + pawn->position.w;
        }
    }

    //if the top edge of rect1 intersects rect2 vertically
    if (rect1Top < rect2Bottom && rect1Top > rect2Top) {
        //if rect1 and rect2 are on top of each other
        if (rect1Right > rect2Left && rect1Right < rect2Right || rect1Left < rect2Right && rect1Left > rect2Left) {
            pawn->posY = rect2Bottom;

            rect1Top = pawn->posY;
            rect1Top = rect1Top + pawn->position.h;
        }
    }
}

我无法理解为什么pawn的上边缘不会碰撞if / else的每次可能的迭代,如果已经尝试过以及重新排列if语句的顺序。

我非常渴望能够洞察这个问题以及一双新的眼睛,看看我显然无法做到这一点。

0 个答案:

没有答案