gpt4 book ai didi

c++ - 如何检测像素图边缘透明的两个 QGraphicsPixmapItem 之间的碰撞?

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:21 33 4
gpt4 key购买 nike

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

最佳答案

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

QGraphicsPixmapItem::HeuristicMaskShape

The shape is determine by calling QPixmap::createHeuristicMask(). The performance and memory consumption is similar to 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中找到

关于c++ - 如何检测像素图边缘透明的两个 QGraphicsPixmapItem 之间的碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803820/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com