PDA

View Full Version : Collision detection QGraphicsItem



Blade
1st January 2007, 19:32
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?

Gopala Krishna
2nd January 2007, 11:42
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
QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode 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.

Blade
2nd January 2007, 13:11
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:
http://www.wormux.org/wiki/images/5/52/ninja_rope.jpg
(open source worms)

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

Methedrine
2nd January 2007, 14:23
It seems like you only know how to do rectangular collision checks, maybe you want to read up on some articles about collision detection (http://www.gamedev.net/reference/list.asp?categoryid=45#199) 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
QGraphicsItem::collidesWithPath(const QPainterPath& path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

wysota
2nd January 2007, 14:42
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 :)

Blade
5th January 2007, 10:20
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!