Results 1 to 2 of 2

Thread: Collision detection between two subclassed QGraphicsItem's

  1. #1
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Collision detection between two subclassed QGraphicsItem's

    Hello,

    I am new to QT and am using 5.5 on a Windows 7 box.

    I have derived two separate classes from QGraphicsItem and I wish to detect when one collides with the other. One is called Particle, the other is called Vector. When a Vector collides with a particle, if the line().p1() or p2() is anywhere within the rectangle of the Particle, I wish to set the parent item of the vector to be the particle and assign the particle pointer to a member variable of the vector. After that the particle will move all attached vectors as a system when I move it around the screen.

    The problem I am encountering is how to determine that the colliding object is a Particle or just another object of some other kind. I thought that using the UserType would be useful here but before I start down a path of code writing, i wanted to see if that is the best solution seeing how I am new to the system and all.

    Regards,
    -Caolan.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Collision detection between two subclassed QGraphicsItem's

    Yes, using proper values return from type() is the suggested option for "detecting" graphic item sub types.
    The qgraphicsitem_cast() function uses that as well.

    Of course you can also use C++ dynamic_cast, but that could be slightly slower.

    Cheers,
    _

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

    Caolan O'Domhnaill (25th September 2015)

Similar Threads

  1. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  2. 2D Race Car collision detection
    By neonnds in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2008, 08:10
  3. Collision detection QGraphicsItem
    By Blade in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2007, 10:20
  4. 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
  •  
Qt is a trademark of The Qt Company.