Results 1 to 7 of 7

Thread: Collision between QGraphicsItem -

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Collision between QGraphicsItem -

    Does it matter if I check collision of a GraphicsItem to every other item in a scene or if I check collision of an item against the parent of these items?

    Ex:
    Option 1:
    Qt Code:
    1. QGraphicsItem generic1;
    2. QGraphicsItem generic2;
    3. [...]
    4. QGraphicsItem generic30;
    5.  
    6.  
    7. QGraphicsItem mainItem;
    8.  
    9. mainItem.collidesWithItem(generic1);
    10. mainItem.collidesWithItem(generic2);
    11. [...]
    12. mainItem.collidesWithItem(generic30);
    To copy to clipboard, switch view to plain text mode 

    Option2:
    Qt Code:
    1. QGraphicsItem genericItemParent;
    2. QGraphicsItem generic1.setParent(genericItemParent);
    3. QGraphicsItem generic2.setParent(genericItemParent);
    4. [...]
    5. QGraphicsItem generic30.setParent(genericItemParent);
    6.  
    7.  
    8. QGraphicsItem mainItem;
    9.  
    10. mainItem.collidesWithItem(genericItemParent);
    To copy to clipboard, switch view to plain text mode 

    Which one is faster?

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision between QGraphicsItem -

    I would partition my scene to minimize the number of objects I am tracking for collision.

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Collision between QGraphicsItem -

    How do I partion the scene? Never heard of that...

  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision between QGraphicsItem -


  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Collision between QGraphicsItem -

    Sorry, "how to do it in QT?" should be the question.
    So, if I mapToScene() my graphicsView viewport rect, then check any colliding items, then check if those items collide with the "mainItem"?

    Example (pseudo-code):
    Qt Code:
    1. QGraphicsItem generic1;
    2. QGraphicsItem generic2;
    3. [...]
    4. QGraphicsItem generic30;
    5.  
    6.  
    7. QGraphicsItem mainItem;
    8. //-------------------------------------//
    9. void addSceneItems(){/* add all items to scene */ }
    10. view.setScene(scene);
    11.  
    12. QPolygonF polygon;
    13. polygon = view.mapToScene(0,0,view.width(),view.height);
    14.  
    15. //--> make poligon "invisible" somehow...
    16.  
    17. void itemVSpoli()
    18. {
    19. for(int i=0;i<scene.items().count();i++)
    20. {
    21. if(scene.itemAt(i).collidesWithItem(polygon))
    22. //send item to function that checks if
    23. // it collides with mainItem.
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Collision between QGraphicsItem -

    So it was simpler than that:
    Qt Code:
    1. QPolygonF poli = sceneView->mapToScene(0,0,sceneView->width(),sceneView->height());
    2. QList<QGraphicsItem *> sceneItems;
    3. sceneItems = scene->items(poli); //this only returns items that are drawn in the screen
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision between QGraphicsItem -

    And why not just do this?
    Qt Code:
    1. QGraphicsItem *myItem = ...
    2. //...
    3. QList<QGraphicsItem*> collisions = scene->collidingItems(myItem);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 11:28
  2. Collision detection QGraphicsItem
    By Blade in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2007, 11:20
  3. Collision handling 101
    By Morea in forum Qt Programming
    Replies: 9
    Last Post: 25th November 2006, 21:17
  4. Painting and collision detection
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 09:57

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.