如何检测pixmaps边缘的两个QGraphicsPixmapItem之间的碰撞是否透明?

问题描述 投票:1回答:1

我有两个QGraphicsPixmapItems,两者的边缘都是透明的,它们不是矩形的。当我尝试使用QGraphicsItem::collidingItems()时,它只检查它们的边界是否发生碰撞。有没有办法只检测非透明部件的碰撞?

c++ qt qt5 collision qpixmap
1个回答
0
投票

您必须将形状模式设置为QGraphicsPixmapItem::HeuristicMaskShape

QGraphicsPixmapItem :: HeuristicMaskShape

通过调用QPixmap :: createHeuristicMask()来确定形状。性能和内存消耗与MaskShape类似


your_item->setShapeMode(QGraphicsPixmapItem::HeuristicMaskShape);

例:

main.cpp中

#include <QApplication>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>

#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;
    QGraphicsScene *scene = new QGraphicsScene;
    w.setScene(scene);

    QList<QGraphicsPixmapItem *> items;

    for(const QString & filename: {":/character.png", ":/owl.png"}){
        QGraphicsPixmapItem *item = scene->addPixmap(QPixmap(filename));
        item->setShapeMode(QGraphicsPixmapItem::HeuristicMaskShape);
        item->setFlag(QGraphicsItem::ItemIsMovable, true);
        item->setFlag(QGraphicsItem::ItemIsSelectable, true);
        items<<item;
    }

    items[1]->setPos(50, 22);
    if(items[0]->collidingItems().isEmpty())
        qDebug()<<"there is no intersection";
    w.show();
    return a.exec();
}

enter image description here

输出:

there is no intersection

完整的示例可以在以下link中找到

© www.soinside.com 2019 - 2024. All rights reserved.