【问题标题】:Precise detection of collision between two graphical items精确检测两个图形项之间的碰撞
【发布时间】:2018-10-09 17:52:42
【问题描述】:

我有两个图形项目,RectItemCurveItem。我需要准确检测这两个项目之间的碰撞。我为展位项目实施了shape() 方法。我验证当我从场景self.collidingItems(self.rect_item, mode=Qt.IntersectsItemShape 中移动其中一个项目时是否检测到碰撞。

我通过在场景中移动它们来碰撞项目,但仅在CurveItem 的顶部检测到碰撞。我不明白我在哪里做错了。

代码:

class RectItem(QGraphicsRectItem):
    def __init__(self, x, y, w, h):
        super().__init__(x, y, w, h)
        self.setFlag(QGraphicsItem.ItemIsMovable)
        self.setPen(QPen(Qt.cyan))

    def shape(self):
        path = QPainterPath()
        path.addRect(self.boundingRect())
        return path


class CurveItem(QGraphicsItem):
    def __init__(self):
        super().__init__()
        self.path = self._setupPath()

    def paint(self, painter, styles, widget=None):
        painter.drawPath(self.path)

    def boundingRect(self):
        return QRectF()

    def shape(self):
        return self.path

    def _setupPath(self):
        path = QPainterPath()
        path.moveTo(0, 0)
        path.cubicTo(99, 0, 50, 50, 99, 99)
        path.cubicTo(0, 99, 50, 50, 0, 0)
        return path


class Scene(QGraphicsScene):
    def __init__(self):
        super().__init__()
        self.curve_item = CurveItem()
        self.rect_item = RectItem(-50, -50, 25, 25)

        self.addItem(self.curve_item)
        self.addItem(self.rect_item)

    def mouseMoveEvent(self, e):
        print(self.collidingItems(self.rect_item, mode=Qt.IntersectsItemShape))
        super().mouseMoveEvent(e)

【问题讨论】:

    标签: python pyqt pyqt4 pyqt5 qgraphicsitem


    【解决方案1】:

    虽然您不会使用boundingRect() 来检测碰撞,但您必须返回一个QRect,它是覆盖shape 的最小尺寸,为此我们可以使用QPainterPathboundingRect() 方法,此外不必覆盖形状QGraphicsRectItem

    class RectItem(QGraphicsRectItem):
        def __init__(self, *args):
            super().__init__(*args)
            self.setFlag(QGraphicsItem.ItemIsMovable)
            self.setPen(QPen(Qt.cyan))
    
    class CurveItem(QGraphicsItem):
        def __init__(self):
            super().__init__()
            self.path = self._setupPath()
    
        def paint(self, painter, styles, widget=None):
            painter.drawPath(self.path)
    
        def boundingRect(self):
            return self.path.boundingRect()
    
        def shape(self):
            return self.path
    
        def _setupPath(self):
            path = QPainterPath()
            path.moveTo(0, 0)
            path.cubicTo(99, 0, 50, 50, 99, 99)
            path.cubicTo(0, 99, 50, 50, 0, 0)
            return path
    
    
    class Scene(QGraphicsScene):
        def __init__(self):
            super().__init__()
            self.curve_item = CurveItem()
            self.rect_item = RectItem(-50, -50, 25, 25)
    
            self.addItem(self.curve_item)
            self.addItem(self.rect_item)
    
        def mouseMoveEvent(self, e):
            print(self.collidingItems(self.rect_item, mode=Qt.IntersectsItemShape))
            super().mouseMoveEvent(e)
    

    【讨论】:

    • 完美运行。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    相关资源
    最近更新 更多