需要有关分离轴定理的帮助

时间:2014-03-23 16:52:11

标签: c++ 2d physics collision

我正在尝试使用SAT检查2个旋转的矩形之间的碰撞。但是在某些角度,它不能正常工作。 (见http://imgur.com/L9ErNUM

如果有人能指出我的错误,会有所帮助。这是我的职责:

bool SeperatingAxisThereom(std::vector<CVector2> rect1_points, std::vector<CVector2> rect2_points)
{
    CVector2 axis;
    float testMagnitude, min1, min2, max1, max2;
    int pointA, pointB;
    for ( int i = 0; i < rect1_points.size(); i++)
    {
        // Get an edge from rect1 and it's normal
        pointA = i;
        pointB = i+1;
        if (pointB >= rect1_points.size()) 
            pointB = 0;
        axis = rect1_points.at(pointA) - rect1_points.at(pointB);
        axis = axis.getPerpendicularVector();
        axis.normalizeVector3D();
        // Project rect 1
        min1 = max1 = axis.dotVector3D(rect1_points[0]);
        for ( int i = 1; i < rect1_points.size(); i++)
        {
            testMagnitude = axis.dotVector3D(rect1_points[i]);
            if (testMagnitude > max1) 
                max1 = testMagnitude;
            if (testMagnitude < min1)
                min1 = testMagnitude;
        }
        // Project rect 2
        min2 = max2 = axis.dotVector3D(rect2_points[0]);
        for ( int i = 1; i < rect2_points.size(); i++)
        {
            testMagnitude = axis.dotVector3D(rect2_points[i]);
            if (testMagnitude > max2)
                max2 = testMagnitude;
            if (testMagnitude < min2)
                min2 = testMagnitude;
        }
        // Check for intersection
        if (min1 < min2) 
        {
            if (max1 < min2 )
            {
                printf("\n");
                return false; // no collision
            }
        }
        else if(min2 < min1)
        {
            if(max2 < min1)
            {
                printf("\n");
                return false; // no collision
            }
        }
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

我不太确定,但我认为你必须根据两种形状的法线来计算轴,而不仅仅是第一种。

从这里采取:http://www.codezealot.org/archives/55

“您必须测试的轴是每个形状边缘的法线。”

似乎你没有在任何地方回归真实?