Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Need Help Regarding Qt GraphicsItem Collision!
Forum Update on Monday, May 27th 2025

Need Help Regarding Qt GraphicsItem Collision!

Scheduled Pinned Locked Moved Game Development
1 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RYELI
    wrote on last edited by
    #1

    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.

    @
    cannonball::cannonball()
    {
    QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
    QTimeLine * timer = new QTimeLine(4000);

    animation->setItem(this);
    animation->setTimeLine(timer);
    
    timer->setLoopCount(0);
    
    for (int i = this->pos().x(); i < 640; ++i)
    {
        animation->setPosAt(i / 640.0, QPointF(i, this->pos().y() ));
        if (hits_enemy())
            timer->setCurrentTime(0);
    }
    
    timer->start();
    

    }
    @

    @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! :(

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved