【问题标题】:Bounding rect to the complex form?将 rect 绑定到复杂的形式?
【发布时间】:2016-11-30 10:02:52
【问题描述】:

我们项目的主题是制作一个模拟 Fusion 的程序。

我们在与我们的 Fusion 类碰撞时遇到了一些问题。我们想为我们的碰撞制作一个复杂的形状。

printScreenFusionProgramm

我们的形状是两个彼此靠近的圆,我们不想有一个边界矩形但形状“复杂”......

这是我们的 Fusion 类

Fusion::Fusion(int x, int y)
{
 this->setPos(x, y);
}

void Fusion::shape(){
//...
}

 void Fusion::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  {

   //set the color
   QBrush brushColor(QColor(Qt::blue));
   painter->setBrush(brushColor);
   painter->setPen(QColor(Qt::blue));
   painter->drawEllipse(0,0,40,40);
   painter->drawEllipse(-20,-20,40,40);
  }

  void Fusion::doCollision()
  {
    // get a new position

    // change the angle with randomness
    if(qrand() %1)
    {
        setRotation(rotation() + (180 + (qrand() % 10)));
    }
    else
    {
        setRotation(rotation() + (180 + (qrand() % -10)));
    }

    // check if the new position is in bounds
    QPointF newPoint = mapToParent(-(boundingRect().width()), -(boundingRect().width() + 2));

    if(!scene()->sceneRect().contains((newPoint)))
    {
        // move back in bounds
        newPoint = mapToParent(0,0);
    }
    else
    {
        // set the new position
        setPos(newPoint);
    }
}

void Fusion::advance(int step)
{
  if(!step) return;

  if(!scene()->collidingItems(this).isEmpty())
  {
    doCollision();
  }

 setPos(mapToParent(0, -1));
}

【问题讨论】:

    标签: qt shape


    【解决方案1】:

    您需要为图形项重新实现“shape”方法以返回对象的实际形状。您可以在 QPainterPath 中返回您想要的任何形状,Qt 将使用它进行碰撞检测。

    【讨论】:

    • 嗨,谢谢你的帮助!!!但是有了这个,我们看起来就像一个屏幕错误 -> bugScreen。如果是因为 shape()... 对这个 bug 有什么想法吗?
    • 图片表明你的boundingRect或者你的shape函数不准确,或者你在一些影响这些方法结果的操作之前没有调用prepareGeometryChange。发生这种情况时,您会看到绘图伪影,因为旧区域未正确清除。
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2019-05-12
    • 2011-08-31
    • 2021-10-06
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多