边界矩形到复杂形式?
Bounding rect to the complex form?
我们项目的主题是制作一个模拟融合的程序。
我们在与 classe Fusion 发生碰撞时遇到了一些问题。我们想为我们的碰撞制作一个复杂的形状。
printScreenFusionProgramm
我们的形状是两个彼此靠近的圆圈,我们不希望有边界矩形但是形状 "complex"...
这是我们的 Fusion class
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));
}
您需要为图形项重新实现 "shape" 方法,以 return 对象的实际形状。您可以 return QPainterPath 中的任何形状,Qt 将使用它进行碰撞检测。
我们项目的主题是制作一个模拟融合的程序。
我们在与 classe Fusion 发生碰撞时遇到了一些问题。我们想为我们的碰撞制作一个复杂的形状。
printScreenFusionProgramm
我们的形状是两个彼此靠近的圆圈,我们不希望有边界矩形但是形状 "complex"...
这是我们的 Fusion class
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));
}
您需要为图形项重新实现 "shape" 方法,以 return 对象的实际形状。您可以 return QPainterPath 中的任何形状,Qt 将使用它进行碰撞检测。