Results 1 to 3 of 3

Thread: QGraphicsItem collision question :)

  1. #1
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem collision question :)

    Hello! I'm glad i found this forum.

    I just started learning Qt a few days ago, and currently i am trying to make a simple game.

    I have read the docs, but maybe i am a bit slow in understanding things so i had to ask a few "newb" questions..
    ( I already run a forum search and checked through all of them that may seem related. )

    So I wanted to make a side-scrolling, with rectangles for the road and the character running on top of them. I made 2 classes, lets say:
    Qt Code:
    1. class Block: public QGraphicsItem
    2. {
    3. ...
    4. }
    5.  
    6. class Character: public QGraphicsItem
    7. {
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    if the Character instance is not standing on top of a Block instance, then it would freefall down.
    My question is, how and where do I define the collision? I've read about
    Qt Code:
    1. virtual bool collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
    To copy to clipboard, switch view to plain text mode 
    I was thinking about implementing this in Character class as method but it needs QGraphicsItem *other as parameter.. :S


    Thank you for your help!

  2. #2
    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: QGraphicsItem collision question :)

    You need some kind of game loop that will periodically run all the logic of the game, checking for collisions and moving items. One of the possibilities is to connect a timer to QGraphicsScene::advance() which will in turn call advance() on each of the items (unless you override the method). There in your character class you can ask the scene for all the items the current item collides with using QGraphicsItem::collidingItems() and make your decisions based upon the result of this call.
    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.


  3. The following user says thank you to wysota for this useful post:

    andzero (9th October 2011)

  4. #3
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem collision question :)

    Hi, thanks for your help!
    Sorry for the late reply.

    I was thinking about that too, glad to know i am in the right direction!

    Now I will work on the game physics and will ask if there's another problem.

Similar Threads

  1. About QGraphicsItem question
    By jiaorenjie in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2011, 08:38
  2. Collision between QGraphicsItem -
    By been_1990 in forum General Discussion
    Replies: 6
    Last Post: 19th November 2010, 12:32
  3. QGraphicsItem pos() method question!
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 19:56
  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.