Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I detect collision among QPixmaps and QGraphicsItems objects

How can I detect collision among QPixmaps and QGraphicsItems objects

Scheduled Pinned Locked Moved General and Desktop
30 Posts 4 Posters 15.9k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andreyc
    wrote on last edited by
    #19

    In this code you take the pixmaps from main_target and create qlist of qgraphicsitems.
    So, the main_target must be populated by the qpixmaps before this code.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DSlim
      wrote on last edited by
      #20

      @ main_targets[0] = QPixmap(":images/darkbluelogo.jpg");
      main_targets[1] = QPixmap(":images/graylogo.jpg");
      main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
      main_targets[3] = QPixmap(":images/lime.jpg");
      main_targets[4] = QPixmap(":images/pink.jpg");
      main_targets[5] = QPixmap(":images/purple.jpg");
      main_targets[6] = QPixmap(":images/redlogo.jpg");
      main_targets[7] = QPixmap(":images/yellow.jpg");
      main_targets[8] = QPixmap(":images/brown.jpg");@

      The main_targets are created and stored in the constructor initially before anything is done. So the qlist should have 9 qpixmaps.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #21

        If I'm not mistaken you need to use .append() method to add elements to the QList. QList::opeartor[] does not work for adding new elements.

        EDIT: Yep, you have to use append() or operator+=() to extend a list "operator[]":http://qt-project.org/doc/qt-5/qlist.html#operator-5b-5d requires an index to be valid (0 <= i < qlist.size())

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DSlim
          wrote on last edited by
          #22

          Thank you finally have it switched over to QGraphicsPixmapItems. Now the initial problem and see if I can get the collision working.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DSlim
            wrote on last edited by
            #23

            Ok the original problem of colliding the two objects. My thinking at the beginning of this post was to have something along the lines of:

            @if(pellets->collidesWithItem(targets){
            remove(pellets)
            remove(targets)

            }@

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #24

              I wouldn't delete the items right away, but only hide them, and delete them later on. But otherwise, it might be something like this.

              Have you taken a look at the Colliding Mice example?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DSlim
                wrote on last edited by
                #25

                Yeah I've looked at that example a lot. Doesn't seem to be in the realm of what I'm trying to accomplish.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #26

                  So you have some QGraphicsItem subclass and come QGraphicsPixmap. You check whether they collide. That part you should be able to take from the example.

                  If they collide, you hide those items and add them to a list of items "to be deleted" (or "to be re-used", if you prefer).

                  You either clean up the list from time to time (using e.g. a timer), or you re-use the items in the list if you need to add a new item (which would be a performance optimization, since creating GraphicsItems isn't free).

                  Where specifically do you have problems?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DSlim
                    wrote on last edited by
                    #27

                    The collision is comparing every item on the screen. It only removes one item not both that collide. It's not item specific.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Asperamanca
                      wrote on last edited by
                      #28

                      If you only want to compare two items, you can use their shape() and QPainterPath::intersects

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DSlim
                        wrote on last edited by
                        #29

                        Is there any way to remove and delete the pellet target from within the QGraphicsRectItem. I'm just hiding the two when they collide, but that doesn't remove it and since the class inherits from the QGraphicsItem base class there isn't a removeitem() function.

                        @class GraphicsCircle : public QGraphicsRectItem
                        // class for the fire bullets
                        {

                        public:
                        GraphicsCircle(qreal dirx, qreal diry)
                        : m_Speed(5)
                        , m_DirX(dirx)
                        , m_DirY(diry)
                        {
                        setRect(-3.0,-3.0,8.0,8.0);
                        setPos(-140, 235);
                        QRadialGradient rGrad( 0.0, 0.0, 20.0, 0.0, 0.0);
                        rGrad.setColorAt(0.0, QColor(255,255,255));
                        rGrad.setColorAt(0.7, QColor(255,255,225));
                        rGrad.setColorAt(1.0, QColor(255,0,0,0));
                        setBrush(QBrush(rGrad) );
                        setPen(QPen(Qt::NoPen));

                            w = 7;
                            h = 7;
                        }
                        
                        QRectF boundingRect() const
                        {
                            qreal shift = 1;
                                return QRectF(-w/2 -shift, - h/2
                                              - shift, w + shift, h + shift);
                        }
                        
                        QPainterPath shape() const
                        {
                            QPainterPath path;
                            path.addRect(boundingRect());
                            return path;
                        }
                        
                        virtual ~GraphicsCircle() {}
                        
                        void advance(int phase)
                        {
                            if(phase == 0) return;
                            setPos(x()+m_Speed*m_DirX, y()+m_Speed*m_DirY);
                        
                        
                            foreach( QGraphicsItem * item, this->collidingItems())
                            {
                                 item->hide();
                                 this->hide();
                        
                            }
                        
                        }@
                        
                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Asperamanca
                          wrote on last edited by
                          #30

                          How about this:

                          @
                          QList<QGraphicsItem*> collisionList = this->collidingItems();
                          qDeleteAll(collisionList);

                          this->hide();
                          // Now it gets tricky: delete this isn't a very good idea within the context of this.
                          // One way to solve it: Derive the class from QObject AND QGraphicsRectItem, and use the deleteLater() call
                          @

                          1 Reply Last reply
                          0

                          • Login

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