如何在Direct2D中应用冲突逻辑?

时间:2018-11-06 01:03:49

标签: visual-c++ game-physics direct2d

我正在使用Direct2D制作平台游戏。我做了一个碰撞逻辑。它运作良好,但平台的角落出现问题。例如。当我的角色跳跃但在拐角处平台顶部的下方时,它将到达平台上方。同样,当我的某些平台从下方推入时,它们也会收集点,但是当我的角色从侧面碰撞并且仅在平台底部附近时,就会触发收集。请告诉我如何改善碰撞逻辑? 这是我的碰撞代码:

D2D1_RECT_F gmsh = g->GetMesh();
//for each wall and platform, check if it falls inside the players bounding box. If yes, check if player is colliding with it
if ((gmsh.left <= boundBox.left && gmsh.right >= boundBox.right) || (gmsh.right <= boundBox.right&&gmsh.right >= boundBox.left))
{

    if (g->DetectCollision(collisionMesh))
    {
     if (collisionMesh.right >= gmsh.left&&collisionMesh.left < gmsh.left && ((collisionMesh.bottom - collisionMesh.top) / 2 + collisionMesh.top  > gmsh.top) && ((collisionMesh.bottom - collisionMesh.top) / 2 + collisionMesh.top < gmsh.bottom ))//handle collision from left
    {
        collisionMesh.right = gmsh.left;
        collisionMesh.left = collisionMesh.right - 11;
        displayMesh.left = collisionMesh.left - 2;
        displayMesh.right = collisionMesh.right + 2;
    }
    else if (collisionMesh.left <= gmsh.right&&collisionMesh.right > gmsh.right && ((collisionMesh.bottom - collisionMesh.top) / 2 + collisionMesh.top > gmsh.top) && ((collisionMesh.bottom - collisionMesh.top) / 2 + collisionMesh.top < gmsh.bottom ))//handle collision from right
    {
        collisionMesh.left = gmsh.right;
        collisionMesh.right = collisionMesh.left + 11;
        displayMesh.left = collisionMesh.left - 2;
        displayMesh.right = collisionMesh.right + 2;
    }
        else if ((collisionMesh.bottom >= gmsh.top) && (collisionMesh.top < gmsh.top) && (collisionMesh.bottom < gmsh.bottom) && ((collisionMesh.right - collisionMesh.left) / 2 + collisionMesh.left + 2 > gmsh.left) && ((collisionMesh.right - collisionMesh.left) / 2 + collisionMesh.left < gmsh.right + 2))//handle collision from top
        {
            collisionMesh.bottom = gmsh.top;
            collisionMesh.top = collisionMesh.bottom - 15;
            displayMesh.bottom = collisionMesh.bottom;
            displayMesh.top = collisionMesh.top;
            ySpeed = 0;
            onGround = true;
        }
        else if ((collisionMesh.top <= gmsh.bottom) && (collisionMesh.bottom > gmsh.top) && (collisionMesh.bottom > gmsh.bottom) && ((collisionMesh.right - collisionMesh.left) / 2 + collisionMesh.left + 2 > gmsh.left) && ((collisionMesh.right - collisionMesh.left) / 2 + collisionMesh.left < gmsh.right + 2))//handle collision from bottom
        {
            collisionMesh.top = gmsh.bottom;
            collisionMesh.bottom = collisionMesh.top + 15;
            displayMesh.bottom = collisionMesh.bottom;
            displayMesh.top = collisionMesh.top;
            ySpeed = 0.5;
            g->MoveUp();
        }

    }
}

g是墙或平台,g-> DetectCollision(...)使用CompareGeomery方法检查重叠。 crashMesh是播放器的D2D_RECT_F对象。 g-> GetMesh()返回D2D_RECT_F,它是墙或平台的边界。 displayMesh是用于显示播放器的D2D_RECT_F。

0 个答案:

没有答案