SFML Trap Collision Detection

I am trying to write a very basic game for my first SFML project. Its a robot with an articulated arm that can only pull balloons with its hand. The problem I am running into is that when checking to see if a pinker sprite crosses with a balloon sprite, it keeps bouncing back, regardless of the placement of the balloon or the ticker robot. I am using transform to place different parts of the robot and this is what is causing the problem I think, but I don't know why. I tried using bounding box collision in a separate program where no transform is used and it works fine. Below is the conversion and detection code. I would appreciate it if someone could explain this to me. I'm brand new to SFML,so I apologize for my ignorance here!

    sf::Transform trBody;
    trBody.translate(sBodyPos);
    /////////////////////////////////////////////////

    sf::Transform trArm1;
    trArm1.translate(sArm1Pos);

    sf::Transform rotArm1;
    rotArm1.rotate(sArm1Rot);

    sf::Transform TR1 = trBody*trArm1*rotArm1;
    /////////////////////////////////////////////////

    sf::Transform trArm2;
    trArm2.translate(sArm2Pos);

    sf::Transform rotArm2;
    rotArm2.rotate(sArm2Rot);

    sf::Transform TR2 = TR1*trArm2*rotArm2;
    /////////////////////////////////////////////////

    sf::Transform trPincer1;
    trPincer1.translate(sPincer1Pos);

    sf::Transform TR3 = TR2*trPincer1;
    /////////////////////////////////////////////////

    sf::Transform trPincer2;
    trPincer2.translate(sPincer2Pos);

    sf::Transform TR4 = TR2*trPincer2;
    /////////////////////////////////////////////////
    sf::Transform trBalloon1;
    trBalloon1.translate(sBalloon1Pos);

    if (sPincer1.getGlobalBounds().intersects(sBalloon1.getGlobalBounds())){

        cout << "Bang" << endl;
        ballOneHit = true;

    }

    // Clear screen
    app.clear();
    app.draw(sArm2, TR2);
    app.draw(sPincer1, TR3);
    app.draw(sPincer2, TR4);
    app.draw(sArm1, TR1);
    app.draw(sBody, trBody);

    if (ballOneHit == false){

        app.draw(sBalloon1, trBalloon1);

    }

    // Update the window
    app.display();

      

+3


source to share


1 answer


Guessing the guesswork, I would say that the rotation of the object is the culprit, since rotation has the unfortunate effect of expanding the boundaries of the object. The bounding box is still in the screen-defined xy plane after rotation, but encloses the object after rotation. As an illustration, look at the global bounding box on the text in this figure:

bounding box rotation

Note that the window surrounds the object, but is not a rotating version of the original bounding box you might expect to get. In words tutorial on graphic transformations :



SFML subjects can provide you with their bounding box. A bounding rectangle is the smallest rectangle that contains an object, with sides aligned on the X and Y axes.

To solve this problem, you can try to create a rectangle that will always be contained within the visible parts of the object, and then use that for hit detection, not the bounding object.

+4


source







All Articles