代码之家  ›  专栏  ›  技术社区  ›  Eugeniu Znagovan

精确检测两个图形项目之间的碰撞

  •  0
  • Eugeniu Znagovan  · 技术社区  · 6 年前

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

    我通过在场景中移动项目来碰撞项目,但仅在 曲线项目 .我不明白我在哪里犯了错误。

    enter image description here

    代码:

    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)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

    虽然您不会使用 boundingRect() 要检测碰撞,必须返回 QRect 这是覆盖 shape ,为此,我们可以使用 boundingRect() 方法 QPainterPath ,此外,无需覆盖形状 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)