PDA

View Full Version : collision detection...



Muffin
7th January 2010, 15:32
Hi

I hava a problem with collision detection. in the picture below the lable Cargo5.5 dosn't detect collision with the QGraphicsPathItem belonging to Cargo6. The outline of the lables is the shape() of the item.

http://www.tiny.cc/ShWEW

the implementation of shape() looks like this



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

and the function where i test for collision, this is where the m_pTextItem e.g Cargo5.5 dosn't detect collision with the QGraphicsPathItem
altough the shape of Cargo5.5 intersects/contains with the QGraphicsPathItem of Cargo6



int SVGPath::TextCollitionCount()
{
int collCount = 0;
QList<QGraphicsItem *> collidingWithList = m_pTextItem->collidingItems(Qt::ContainsItemShape);
collCount = collidingWithList.count();
if(collCount)
{
for(QList<QGraphicsItem*>::iterator it = collidingWithList.begin();it != collidingWithList.end();)
{
if((*it)->type() == SVGPath::Type)
{
SVGPath *item = qgraphicsitem_cast<SVGPath *>(*it);
if(m_pTextItem->collidesWithPath(item->shape(),Qt::ContainsItemShape))
it++;
else
it = collidingWithList.erase(it);
}
}
}
}

is there something i have missed? stacking of items shouldn't matter should it? this application has no paint event etc it's sort of a static presentation (for now) so everyting is added to the scene and later on i set scene to the view.

Regards Muffin www.a-eon.com

Muffin
8th January 2010, 11:28
Qt::ContainsItemShape should not be there, leftovers from previous test.I now also have boundingRect() like this:


QRectF SVGText::boundingRect() const
{
QRectF newRect = QGraphicsTextItem::boundingRect();
newRect = newRect.adjusted(0,4,0,-4);
return newRect;
}
QPainterPath SVGText::shape() const
{
QPainterPath path;
path.addRect(this->boundingRect());
return path;
}

but that doesn't help much, the strange thing is that some lables report collision although they are no way near intersecting and others are clearly intersecting but doesn't detect that. ??? i'm confused :confused: