Important: Please read the Qt Code of Conduct -

Need Help Regarding Qt GraphicsItem Collision!

  • Hi. So I am a complete beginner with Qt. I am tasked to make a Plant vs. Zombie esque game from my professor and I am at complete loss about the collision detection.

    I have been doing Google searches everywhere regarding the use of Qt Timeline, GraphicsItem, GraphicsScene for collision handling and I gave up.

    A short description about my game: There are three sprites on the playing field: Towers, Cannonballs and Mobs. Towers are placed by player to guard the left side of the screen and mobs appear from the right side of the screen. Towers constantly fire cannonballs every few seconds.

    QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
    QTimeLine * timer = new QTimeLine(4000);

    for (int i = this->pos().x(); i < 640; ++i)
        animation->setPosAt(i / 640.0, QPointF(i, this->pos().y() ));
        if (hits_enemy())


    @bool cannonball::hits_enemy()
    for (int i=0; i<this->collidingItems().count(); i++)
    if (qgraphicsitem_cast<mobs*>(this->collidingItems()[i]) ) return true;

    return false;


    Basically, those are some codes from the cannonball class.

    I would like the cannonball upon collision to a mob sprite to disappear, damage the mob and fire again once the cooldown cleared.

    I am pretty desperate right now as I have been looking for similar solutions for over 6 hours with no solid result. Please help! :(

Log in to reply