Results 1 to 8 of 8

Thread: QGraphicsPixmapItem collision detection

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QGraphicsPixmapItem collision detection

    I add two QGraphicsPixmapItem objects in a QGraphicsScene, and I want to detect the collision between them. The picture's format is PNG. But when collision happens, the system use the outer rectangle to detect it, not the inner outline of the items.
    Question is, how can I detect the collision according to the inner outline of the picture, not the outer rectangle of the picture.

    Thanks in advance!

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    Hi!

    You need to subclass and reimplement either of

    Qt Code:
    1. QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode)
    2. QGraphicsItem::shape()
    To copy to clipboard, switch view to plain text mode 

    whichever is faster in your case.

    Johannes

  3. #3
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    Quote Originally Posted by JohannesMunk View Post
    Hi!

    You need to subclass and reimplement either of

    Qt Code:
    1. QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode)
    2. QGraphicsItem::shape()
    To copy to clipboard, switch view to plain text mode 

    whichever is faster in your case.

    Johannes
    Thanks!!

    But I thought QGraphicsPixmapItem has done that for me.
    The sub-attack demo in Qt4.6 just use this class to detect collision, not having reimplemented both methods.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    That's right. But the default implementation obviously returns the outer rectangle. If you want something different, here is the place to change it.

    Joh

  5. #5
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    Quote Originally Posted by JohannesMunk View Post
    That's right. But the default implementation obviously returns the outer rectangle. If you want something different, here is the place to change it.

    Joh
    Maybe that's the problem: I do not know how to reimplement the two methods, because I have no idea how to get the inner outline of the PNG picture and return it as QPainterPath, any idea?

  6. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    That's an entirely different question! And I have no simple answer ready for you.

    What exactly do you mean with inner outline? Is your PNG partly transparent?

    Joh

  7. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    I just found this:

    http://doc.trolltech.com/latest/qgra...mode-enum.html

    So if you set the transparency right, that should work!

    Excerpt of the source code of how this is implemented internally :->

    Qt Code:
    1. void updateShape()
    2. {
    3. shape = QPainterPath();
    4. switch (shapeMode) {
    5. case QGraphicsPixmapItem::MaskShape: {
    6. QBitmap mask = pixmap.mask();
    7. if (!mask.isNull()) {
    8. shape = qt_regionToPath(QRegion(mask).translated(offset.toPoint()));
    9. break;
    10. }
    11. // FALL THROUGH
    12. }
    13. case QGraphicsPixmapItem::BoundingRectShape:
    14. shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height()));
    15. break;
    16. case QGraphicsPixmapItem::HeuristicMaskShape:
    17. #ifndef QT_NO_IMAGE_HEURISTIC_MASK
    18. shape = qt_regionToPath(QRegion(pixmap.createHeuristicMask()).translated(offset.toPoint()));
    19. #else
    20. shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height()));
    21. #endif
    22. break;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Joh
    Last edited by JohannesMunk; 4th July 2010 at 19:39.

  8. #8
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem collision detection

    Quote Originally Posted by JohannesMunk View Post
    That's an entirely different question! And I have no simple answer ready for you.

    What exactly do you mean with inner outline? Is your PNG partly transparent?

    Joh
    yes, the PNG pictures are partly transparent in the edges. Does Qt provide any method to get the inner outline. I thought QPixmap or some other iamge-operating class should have that kind method.

Similar Threads

  1. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  2. turn off collision detection?
    By Deacon in forum Qt Programming
    Replies: 14
    Last Post: 30th December 2008, 17:37
  3. 2D Race Car collision detection
    By neonnds in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2008, 08:10
  4. Collision detection QGraphicsItem
    By Blade in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2007, 10:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.