Search code examples
c++collision-detectiongame-physicssfml

Switch Statement within For Loop Failing to Check Variable at Certain Values


As the title suggests, I am experiencing problems with a switch statement failing to check the i variable within a for loop a certain values. The Boolean function below simulates oriented bounding box collision between two sprites, with the i variable representing each face of the oriented bounding box. However, when the function returns false, i is only checked at values 0 and 1, and when the function returns true, i is only checked at values 2 and 3.

I don't know whether or not it's the for loop or the switch statement that is causing this error, and I've double-checked to see if the collision works on all the faces of the oriented bounding boxes, to which I can confirm that it indeed works perfectly. Any form of assistance would be gladly appreciated!

Collision.cpp:

bool Collision::orientedBoundingBoxTest(const sf::Sprite &object1, const sf::Sprite &object2, int object1Width, int object1Height, int object2Width, int object2Height)
{
    /*Variables assigned and declared here*/

    for (int i = 0; i < 4; i++)
    {
        float faceX = collisionObject1.getPoint(i).x - collisionObject1.getPoint((i + 1) % 4).x;
        float faceY = collisionObject1.getPoint(i).y - collisionObject1.getPoint((i + 1) % 4).y;

        float perpendicularAxisX = -faceY;
        float perpendicularAxisY = faceX;

        float normalizedAxisLength = sqrt(perpendicularAxisX * perpendicularAxisX + perpendicularAxisY * perpendicularAxisY);
        perpendicularAxisX /= normalizedAxisLength;
        perpendicularAxisY /= normalizedAxisLength;

        float object1Min = std::numeric_limits<float>::max(), object1Max = -object1Min;
        float object2Min = std::numeric_limits<float>::max(), object2Max = -object2Min;

        for(int j = 0; j < 4; j++)
        {
            float object1Projection = (perpendicularAxisX * (collisionObject1.getPoint(j % 4).x + collisionObject1.getPosition().x) + perpendicularAxisY * (collisionObject1.getPoint(j % 4).y + collisionObject1.getPosition().y)) / (perpendicularAxisX * perpendicularAxisX + perpendicularAxisY * perpendicularAxisY);
            object1Min = min(object1Projection, object1Min);
            object1Max = max(object1Projection, object1Max);
        }

        for(int j = 0; j < 4; j++)
        {
            float object2Projection = (perpendicularAxisX * (collisionObject2.getPoint(j % 4).x + collisionObject2.getPosition().x) + perpendicularAxisY * (collisionObject2.getPoint(j % 4).y + collisionObject2.getPosition().y)) / (perpendicularAxisX * perpendicularAxisX + perpendicularAxisY * perpendicularAxisY);
            object2Min = min(object2Projection, object2Min);
            object2Max = max(object2Projection, object2Max);
        }

        switch (i) //Switch statement failing to check "i" variable at certain values
        {
        case 0:
            deltaY = -(object1Max - object2Min);
            break;
        case 1:
            deltaX = object1Min - object2Max;
            break;
        case 2:
            deltaY = object1Min - object2Max;
            break;
        case 3:
            deltaX = -(object1Max - object2Min);
            break;
        }

        if (! (object1Max >= object2Min && object1Min <= object2Max))
            return false;
    }
    return true;
}

Engine.cpp:

void Engine::physics(sf::RenderWindow& _window)
{
    for (int i = 0; i < 5; i++)
    {
        if (collision.orientedBoundingBoxTest(collisionArray[i].collisionObjectSprite, player.playerSprite, collisionArray[i].width, collisionArray[i].height, player.width, player.height))
        {
            currentCollisionObjectIndex = i;
            player.hitTest = true;
            break;
        }
        else
        {
            player.hitTest = false;
        }
    }

    /*Additional code here*/
}

Solution

  • In my opinion, your conclusion is wrong, put cout << i << endl; between cases, and ensure about your results.

    I'm sure each 0..3 will be meet on each iterate ot i, unless the return statement be invoked.

       switch (i)
        {
        case 0:
            deltaY = -(object1Max - object2Min); cout << i << endl;
            break;
        case 1:
            deltaX = object1Min - object2Max; cout << i << endl;
            break;
        case 2:
            deltaY = object1Min - object2Max; cout << i << endl;
            break;
        case 3:
            deltaX = -(object1Max - object2Min); cout << i << endl;
            break;
        }