Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Collisoin detection with CustomGraphicItem
QtWS25 Last Chance

Collisoin detection with CustomGraphicItem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.3k 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.
  • W Offline
    W Offline
    Winny
    wrote on last edited by
    #1

    Hi guys,
    i'm relative new to programming so please be patient with me.
    I got a Problem with collision dectection in QGraphicsitem or / and QGraphicscene.
    I wrote a new class which is a QGraphicsItem handeled by a Scene.
    If i want to detect colliding items it doesn't work proparly.

    I would like to get the length for the intersecting rectangles.

    here i check my colliding items
    @void MainWindow::on_simulation_released()
    {
    QList <QGraphicsItem*> ert = scene->items();

    qDebug() << scene->items();
    qDebug() << scene->collidingItems(scene->items().at(0));
    // Räume die untersucht werden
    Raum* room[100];
    // Schleife zur Suche der Räume
    for (int i=0 ; i < ert.length(); i++)
    {
       if (ert.at(i)->type() == 65536+1)
       {
          room[i] = qgraphicsitem_cast<Raum*>(ert.at(i));
          qDebug() << room[i]->collidingItems();
          QPainterPath p = room[i]->shape();
          qDebug() << p.length();
    
       }
    
    };
    

    }
    @

    To easier realise what my problem is:
    http://s7.directupload.net/images/130408/wmswb8xr.jpg

    This is my Custom Item

    @#include "raum.h"

    Raum::Raum(int l, int b, int laengescene, int breitescene)
    {
    pressed = false;
    setFlag(ItemIsMovable);
    laenge = l;
    breite = b;
    flaeche = (l*b);
    scenebreite = breitescene;
    scenelaenge = laengescene;
    temperatur = 10 + (rand());

    }

    QRectF Raum::boundingRect() const
    {
    return QRectF(-laenge,-breite,laenge,breite);

    }

    QPainterPath Raum::shape() const
    {
    QPainterPath path;
    path.addRect(this->boundingRect().adjusted(4,4,-4,-4));
    return path;
    }

    void Raum::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);

    }

    void Raum::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
    pressed = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
    acceptedarea();
    update();
    }

    void Raum::acceptedarea()
    {
    if (this->x()-laenge/2 <= -scenelaenge) this->setPos(-scenelaenge+laenge,this->y());
    if (this->x()+laenge/2 >= 0) this->setPos(0,this->y());
    if (this->y()-breite/2 <= -scenebreite) this->setPos(this->x(),-scenebreite+breite);
    if (this->y()+breite/2 >= 0) this->setPos(this->x(),0);
    int h = this->x();
    int j = this->y();
    if (h% 50 > 25)
    {
    this->moveBy(hP,0);
    }
    else
    {
    this->moveBy(-(hP),0);
    }
    if (j% 50 > 25)
    {
    this->moveBy(0,jP);
    }
    else
    {
    this->moveBy(0,-(jP));
    }

    }

    void Raum::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    QRectF rec = boundingRect();
    QPen pen (Qt::red);
    painter->setPen(pen);
    painter->drawRect(rec);
    }
    @

    thx Guys for any help
    Sorry for my english its not the best

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bodzio131
      wrote on last edited by
      #2

      Hi,

      Are you sure that this adjustment is ok?:
      @path.addRect(this->boundingRect().adjusted(4,4,-4,-4));@

      Let's say you have top-left point (10,20) and right-bottom (20,10) then adjusted version is (14,24) and (16,6), which is probably not what you want.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Winny
        wrote on last edited by
        #3

        Works perfectly fine thanks.

        Big hug

        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