Results 1 to 5 of 5

Thread: determining collision of item groups

  1. #1
    Join Date
    Feb 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default determining collision of item groups

    In my problem, I'm creating item groups which consist of a number of QGraphicsRectItem rectangles. That is, each group is a complex shape made of several overlapping rectangles. Call these "rectangle groups."

    Next, I want to determine if two "rectangle groups" are colliding. I thought, "Hey, use collidesWithItem()"

    So if I have group1 (which is a QGraphicsItemGroup) and also group2, I could write

    group1.collidesWithItem(group2)

    Well, the problem is that to determine collision it is checking the overall bounding rectangle of each group. It is not looking at it rectangle-by-rectangle. Potentially two groups could "nestle" in close to each other without any of their sub-rectangles overlapping, but that is not what it is checking.

    I could write code that loops on every pair of sub-rectangles, but I'm actually working in PyQt and I'm trying to avoid writing loops in Python.

    Just wondering if anyone has an idea how to do what I want without resorting to custom functionality.

    Thanks,
    Mike

  2. #2
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: determining collision of item groups

    Please tell me you found the solution... I need it

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: determining collision of item groups

    Since the OP hasn't posted anything here in over 7 years, I doubt he is going to answer your question.

    As the OP states, QGraphicsItem::collidesWithItem() will probably not work well, since it uses the bounding rectangles for each of the groups. You could try using the QGraphicsItem::boundingRegion() method to retrieve the QRegion for each group and calling QRegion::intersects() to see if the two QRegions are overlapping. However, QGraphicsItemGroup does not re-implement boundingRegion(), so it might simply return boundingRect() as a QRegion so you would be out of luck there. You will have to test this.

    If you have to look at each child item inside the two groups, you can do a little bit of optimizing. If collidesWithItem() return false, then you know that there cannot be any collision between any child items in the groups.

    If there is a collision between the two groups, you do not have to examine every pair of children in each group. You have to examine only the children which lie within the intersection of the two group bounding rects (QRectF::intersected()).
    Last edited by d_stranz; 29th November 2017 at 22:47.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: determining collision of item groups

    Thanks for your answer. I am kind of new to Qt and I wasn't sure if the QGraphicsItemGroup collision was a bug in my code, or it always uses the bounding rectangle.

    My current solution is using a QGraphicsScene instead of QGraphicsItemGroup, that way the collision detection works perfectly. It's not an optimal solution, but hey, it works right?

    The only problem is that I cannot have the same item in two QGraphicsScenes, so I end up using two copies of the same item, one in the scene connected with the view, and one in the scene representing the group, for collision detection.

    in the function definition of QGraphicsScene::addItem(QGraphicsItem *item) it is stated:

    If the item is already in a different scene, it will first be removed from its old scene, and then added to this scene as a top-level.

    Is there a workaround for this, so that the same model could be shared by multiple scenes?

    Also thanks for your solution. I will try it out and post an update.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: determining collision of item groups

    The only problem is that I cannot have the same item in two QGraphicsScenes, so I end up using two copies of the same item, one in the scene connected with the view, and one in the scene representing the group, for collision detection.
    This seems like a very strange solution. The whole idea of scenes is that they are meant to be shared among multiple views, and it is the view that determines which part of the scene it displays and at what scale. You are turning this idea upside-down by using two different scenes but only one view.

    QGraphicsItem instances have the same kind of parent-child relationships that QWidgets do: they can have only one parent, and if you try to add them to more than one parent as a child, they will be removed from the first parent and become "owned" by the second parent instead.

    I think a better solution would be to derive a new class from QGraphicsItemGroup and override the collidesWithItem() method. All of your item groups will be instances of this new class, and all methods remain the same as defined in QGraphicsItemGroup except the overridden one. In the new method, you do what I suggested: first, determine if the overall bounding rects intersect (by calling the QGraphicsItemGroup implementation). If they don't you're done. If they do, then depending on whether the other colliding item is a single item or another group, you check each item in your group to see if it intersects with the other item or with a member of the other group.

    The overhead of trying to keep the items in two scenes synchronized has to be at least as complex as an optimized algorithm for detecting collisions between the members of two item groups.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Determining the height of an item in a QListWidget
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2009, 19:18
  2. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  3. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

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.