简体   繁体   中英

SFML Bounding Box Collision Detection

I am trying to write a very basic game for my first SFML project. Its a robot with a jointed arm that will be able to pop balloons with his hand only. The problem I am having is that when checking to see if the pincer sprite intersects with the balloon sprite , it returns true all the time, regardless of the placement of the balloon or the robot pincer. I am using transform to place the various parts of the robot arm, and this is whats causing the problem I think, but I don't know why. I have tried using bounding box collision in a separate program where transform is not used and it worked perfectly. The transformation and detection code is below. I would be grateful if anyone could explain this to me.I am brand new to SFML so apologies 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();

Hazarding a guess, I'd say that rotation of the object is the culprit, as rotation has the unfortunate effect of expanding the bounds of an object. The bounding rectangle after rotation is still in the xy plane determined by the screen, but encompasses the object post-rotation. As illustration, look at the global bounding box on text in this picture:

边界框旋转

Note that the box surrounds the object, but isn't the rotated version of the original bounding box that you might expect to get. In the words of the tutorial on graphics transforms :

SFML entities can give you their bounding box. The bounding box is the minimal rectangle that contains the entity, with sides aligned on the X and Y axes.

To solve this issue, you might try creating a rectangle that will always be wholly contained inside the visible parts of the object, and then use that for hit detection instead of the object's bounding box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM