Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Collision detection?
Forum Update on Monday, May 27th 2025

Collision detection?

Scheduled Pinned Locked Moved Game Development
7 Posts 2 Posters 11.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thehseen
    wrote on 1 Nov 2012, 19:03 last edited by
    #1

    I have two GraphicsItem objects added to a GraphicsScene.
    Their flags have been set to "ItemIsMovable", so the user can move them about in the scene.
    How do I use the collision detection feature of Qt to check if there is a collision when the items are being moved by the user.
    I know you have to use the collidesWith() method.
    Not sure exactly how to use it,

    here is my code so far:

    @ QGraphicsPixmapItem *picture = new QGraphicsPixmapItem(QPixmap("C:/Users/Public/Pictures/images (2)"));
    QGraphicsPixmapItem *picture2 = new QGraphicsPixmapItem(QPixmap("C:/Users/Public/Pictures/images (5)"));

    picture2->setPos(500,500);
    
    picture->setFlag(QGraphicsItem::ItemIsMovable);
    picture2->setFlag(QGraphicsItem::ItemIsMovable);
    scene.addItem(picture);
    scene.addItem(picture2);
    qDebug()<< picture << endl <<  picture;
    QGraphicsView view(&scene);
    view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    view.show();@
    

    Mohammed Thehseen Shah

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thehseen
      wrote on 1 Nov 2012, 19:14 last edited by
      #2

      Does the collision detection happen automatically behind the scenes and is there a signal generated
      that I can slot into.

      Mohammed Thehseen Shah

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thehseen
        wrote on 1 Nov 2012, 19:18 last edited by
        #3

        Oh crap graphics scene is not a descendant of QWidget or QObject, therfeore no signals and slots.
        Which confuses me even more. How does it work then?

        Mohammed Thehseen Shah

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thehseen
          wrote on 1 Nov 2012, 19:58 last edited by
          #4

          By the way all of that code is in main.cpp so I haven't sub-classed any thing

          Mohammed Thehseen Shah

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thehseen
            wrote on 1 Nov 2012, 20:44 last edited by
            #5

            Ok so from reading a bit of the qt documentation I have figured out that I have to call someItem.collidingItems(), to give me a list of items that have collided with the item "someItem".
            I think I am going to check if there is a collision everytime I release the mouse and then if I have moved one item over another there should be collision which I can respond to.

            My problem is I don't know how to handle events outside of the signal slot paradigm.
            I have only ever used signal and slots in my simple gui's to handle user clicks on a button or if a user toggles a checkbox etc.
            With the Event system when a user actually drags an item and then releases it then how does the system handle the event because no signal is generated since GraphicsItems are not QObjects or QWidgets?

            Mohammed Thehseen Shah

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jake007
              wrote on 1 Nov 2012, 20:44 last edited by
              #6

              You can check with function collidesWithItem()

              There is a nice example program (Diagram Scene) in examples ( file arrow.cpp, method paint).
              It returns bool true if it does collide or false if it does not.

              And in graphics scene you have signals and slots. You have change signal based on which you can process. Take a look at docs "here":http://qt-project.org/doc/qt-4.8/QGraphicsScene.html

              If you want signals for collision, you'll have to derive QGraphicsScene class and re implement mouseMoveEvent() or similar function, and then emit the signal.

              And next time please use the edit button and modify your last post if there are any changes.

              Regards,
              Jake


              Code is poetry

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thehseen
                wrote on 1 Nov 2012, 23:47 last edited by
                #7

                Jake, you the man!

                Yes Graphics Scene does have signals and slots and I am an idiot. I don't know how I came to the conclusion that it doesn't.

                So now I have sub classed QGraphicsScene and I have re-implemented the
                updateChange (const QList<QRect> rects) slot where I detect collisions by examining the area of the scene that has changed by getting all the items in the region that has changed and checking for any collisions.
                I connected the sceneChanged(const QList<QRect> & rects) signal to it obviously.

                Now I am going to Try and re-implement mouseMoveEvent()

                Mohammed Thehseen Shah

                1 Reply Last reply
                0

                1/7

                1 Nov 2012, 19:03

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved