Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bounding Box Collision Detection  (Read 3977 times)

0 Members and 1 Guest are viewing this topic.

neverbend007

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Bounding Box Collision Detection
« on: December 03, 2014, 03:29:14 pm »
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 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();



 
« Last Edit: December 03, 2014, 03:46:58 pm by neverbend007 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Bounding Box Collision Detection
« Reply #1 on: December 03, 2014, 05:34:19 pm »
Do you initialize the ballOneHit? Do you get the boom printed out? If the boolean isn't initialized it might contain true from the beginning.

You should read the offical tutorials. I don't think you understand how the translation stuff works. Sprites have their own translation functions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

neverbend007

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Bounding Box Collision Detection
« Reply #2 on: December 03, 2014, 06:23:29 pm »
Hi, yeah ballOneHit is initialised to false when it is declared. My robot displays and functions perfectly, all three sections of his arm work as they should. When the game window opens, I can only see the robot, no balloon, but that is because the if statement is true so ballHitOne immediately becomes true and so the balloon is never drawn.

I was going by the instructions on this page:

http://sfml-dev.org/tutorials/2.0/graphics-transform.php

Am I misunderstanding them or am I looking a the wrong section completely?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Bounding Box Collision Detection
« Reply #3 on: December 03, 2014, 09:32:58 pm »
Since you didn't provide a minimal and complete example it's nearly impossible to tell, whether things are used properly or not, however since everything is drawn right, I guess we can assume your "math" is correct. ;)

My next guess would be that you assume, the bounding box would transform with the object. This is partially true, however the bounding box provided by SFML is always axis aligned (see the tutorial). Thus even if the shapes visually do not intersect, their axis aligned bounding boxes still might.

If that didn't solve anything, you'll have to remove everything unrelated and provide a minimal and complete example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/