PDA

View Full Version : Collision detection between two subclassed QGraphicsItem's



Caolan O'Domhnaill
24th September 2015, 23:07
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.

anda_skoa
25th September 2015, 11:02
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,
_