碰撞后物体卡住了

时间:2013-12-01 09:51:23

标签: c++ opengl glut

我有两个矩形对象,它们来自一个点并使用鼠标移动。碰撞代码完美无缺,但当物体碰撞时,物体突然变得不可移动。

//Here is my collision code
bool collision(GLPoint A, GLPoint B)
{
    //Width/Height
    int w = 150;
    int h = 150;

    //Sides of A Rectangle
    int leftA = A.x;
    int rightA = A.x+w;
    int topA = A.y;
    int bottomA = A.y+h;

    //Sides of B Rectangle
    int leftB = B.x;
    int rightB = B.x+w;
    int topB = B.y;
    int bottomB = B.y+h;

    if(bottomA <= topB)
        return false;
    if(topA >= bottomB)
        return false;
    if(rightA <= leftB)
        return false;
    if(leftA >= rightB)
        return false;

    return true;
    /*
    if(ALeft <= BRight)
        return true;
    else
        return false;*/
}

这是我的代码,如果鼠标在矩形内,我移动对象。如果鼠标没有碰撞,我可以移动它。但是,当对象与另一个矩形碰撞时,它会卡住。这是物体碰撞前的照片。 http://imgur.com/3GQnO0x

碰撞后:http://imgur.com/219TgFL

static int x=iarray[0].x;
static int y=iarray[0].y;
//Set points of 1st Instance
if(mouseX > iarray[0].x && mouseX < iarray[0].x +150 && 750-mouseY < iarray[0].y && 750-mouseY > iarray[0].y-150)
{
    if(collision(iarray[0], iarray[1]))
    {
        cout << "COLLIDING" << endl;
    }
    else
    {
        cout << "NOT COLLIDING" << endl;
        x = mouseX-75, y = 750-mouseY+50;               
    }

    iarray[0].set(x,y);
}

1 个答案:

答案 0 :(得分:0)

您应该检查将来的位置并决定是否可以移动,而不是检查当前位置。换句话说:使边界矩形更大但不是你绘制的实际东西。

相关问题