PDA

View Full Version : QGraphicsItem collision question :)



andzero
8th October 2011, 19:20
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:

class Block: public QGraphicsItem
{
...
}

class Character: public QGraphicsItem
{
...
}
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

virtual bool collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
I was thinking about implementing this in Character class as method but it needs QGraphicsItem *other as parameter.. :S


Thank you for your help! :)

wysota
9th October 2011, 12:07
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.

andzero
9th October 2011, 14:31
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. :D