0

I'm trying to create collision for my OpenGL application.

I have code that successfully tests whether my camera is inside my platform object:

void checkInsidePlatform()
{
float halfW = gymPlatform -> getW() / 2;
float Height = gymPlatform -> getH();
float halfD = gymPlatform -> getD() / 2;
float platformRight = gymPlatform -> getX() + halfW + 1;
float platformTop = gymPlatform -> getY() + Height + 1;
float platformFront = gymPlatform -> getZ() - halfD - 1;

if(testPlatformCollision())
{
    //Below code doesnt work (NEED HELP HERE)
    if(myCamera -> curPos -> x < platformRight)
    {
        myCamera -> curPos -> platformRight;
    }
    if(myCamera -> curPos -> z > platformFront)
    {
        myCamera -> curPos -> platformFront;
    }
    if(myCamera -> curPos -> y < platformTop)
    {
        myCamera -> curPos -> platformTop;
    }
}
}

bool testPlatformCollision()
{
float halfW = gymPlatform -> getW() / 2;
float Height = gymPlatform -> getH();
float halfD = gymPlatform -> getD() / 2;
float platformLeft = gymPlatform -> getX() - halfW - 1;
float platformRight = gymPlatform -> getX() + halfW + 1;
float platformTop = gymPlatform -> getY() + Height + 1;
float platformFront = gymPlatform -> getZ() - halfD - 1;
float platformBack = gymPlatform -> getZ() + halfD + 1;

if((myCamera -> curPos -> x > platformLeft) && (myCamera -> curPos -> x < platformRight))
{
    if((myCamera -> curPos -> z > platformFront) && (myCamera -> curPos -> z < platformBack))
    {
        if(myCamera -> curPos -> y < platformTop)
        {
            return true;
        }   
    }
}

return false;
}

But now I'm stuck. I'm not sure how to move the camera outside of the platform if it goes inside. If the camera is inside the platform, all 3 tests are performed.

4

1 回答 1

2

您需要执行冲突解决。冲突解决是解决冲突的行为,并且比仅仅执行布尔IsColliding函数要复杂得多。

要搜索的其他信息是:分离轴测试 (SAT)。由于您正在处理 AABB(大概),您可以很容易地组合一个简单的分辨率,只需将您的相机移到外面。

这是一个简单的描述:找到相机应该移动的方向,使其在框外。这个方向应该是可以向外移动的最短路径。找到要移动的距离,然后执行该移动操作。

当然,实际的实现会涉及更多。

于 2013-05-14T21:08:14.250 回答