Collisoin detection with CustomGraphicItem



  • 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



  • 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.



  • Works perfectly fine thanks.

    Big hug


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.