将复数形式绑定到矩形?
问题描述:
我们项目的主题是制作一个模拟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));
}
答
您需要重新实现图形项目的“形状”方法以返回对象的实际形状。你可以在QPainterPath中返回你想要的任何形状,Qt将使用它来进行碰撞检测。
嗨,谢谢你的帮助!但有了这个,我们是一个屏幕截图 - > [bugScreen](http://i.imgur.com/P5ib3lL.png)。 Idk如果因为shape()...对这个bug有任何想法吗? –
图片表明您的boundingRect或形状函数不准确,或者在影响这些方法的结果的某些操作之前未能调用prepareGeometryChange。当发生这种情况时,您会看到绘制工件,因为旧区域未被正确清除。 – goug