Results 1 to 6 of 6

Thread: Collision detection QGraphicsItem

  1. #1
    Join Date
    Jan 2007
    Posts
    3

    Default Collision detection QGraphicsItem

    Hi guys!

    I want to programm a little game in worms style. Now I need to check collisions between my Characters and my foreground picture(map).
    What i don't know is how to implement collision detection with the map(is also a QGraphicsItem) as no rectangle.
    I wanted to do this by writing a new QGraphicsItem::shape() function. But for this i need to do a QPainterPath from my map and I think theres no function to do a QPainterPath of a QPixmap?

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Collision detection QGraphicsItem

    Quote Originally Posted by Blade View Post
    Hi guys!

    I want to programm a little game in worms style. Now I need to check collisions between my Characters and my foreground picture(map).
    What i don't know is how to implement collision detection with the map(is also a QGraphicsItem) as no rectangle.
    I wanted to do this by writing a new QGraphicsItem::shape() function. But for this i need to do a QPainterPath from my map and I think theres no function to do a QPainterPath of a QPixmap?
    You can implement
    Qt Code:
    1. QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode)
    To copy to clipboard, switch view to plain text mode 
    and check whether other item is the item you wanted to check for collision and take necessary action (if you know the constraints properly). This may sometimes increase the performance too without need to reimplement shape().

    Easier suggestion would be to implement shape() and manually return the boundary(as QPainterPath). This shouldn't be that difficult if your pixmap is not very complicated since you just need to return path in local coordinates. Remember you should just return boundary of your item.

  3. #3
    Join Date
    Jan 2007
    Posts
    3

    Default Re: Collision detection QGraphicsItem

    when i use collidingWithItem the result is the same. I want to move my character on the map and so i need exact collision detection with the map.

    i want to do a shape of maps like this:

    (open source worms)

    i think that making a path of this without a function would be difficult, or?

  4. #4
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision detection QGraphicsItem

    It seems like you only know how to do rectangular collision checks, maybe you want to read up on some articles about collision detection to get a deeper insight into the different ways to check for collisions.

    EDIT: It seems like Qt has some cd algorithms implemented already so that you might want to use
    Qt Code:
    1. QGraphicsItem::collidesWithPath(const QPainterPath& path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
    To copy to clipboard, switch view to plain text mode 
    Last edited by Methedrine; 2nd January 2007 at 14:27.

  5. #5
    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 detection QGraphicsItem

    Quote Originally Posted by Blade View Post
    I wanted to do this by writing a new QGraphicsItem::shape() function.
    Yes, that's a correct approach.
    But for this i need to do a QPainterPath from my map and I think theres no function to do a QPainterPath of a QPixmap?
    You have to find a way to scan your pixmap and create a path for it. You can for example try to do that by first detecting edges of the shape by using high frequency filtering algorithms (like Sodel, Laplace or even a simple convolution filter) and then approximate the shape by "walking" through the edge. Simply speaking - you need to trace the image

  6. #6
    Join Date
    Jan 2007
    Posts
    3

    Default Re: Collision detection QGraphicsItem

    Thank you!
    I thought perhaps there is already a solution included in Qt! But I think I'll read the articles you mentioned above and I hope it will work some time!

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. QGraphicsItem and signals
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 11:19
  3. Painting and collision detection
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 08: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.