0
在Screen上,黃色球接近黃色弧線並擊中它。每個弧(黑色,綠色和黃色是弧 - 不同的對象)。當球擊中黃色弧線時碰撞檢測顯示3個物體 - 3弧線,即使它僅擊中一個黃色弧線。碰撞檢測不能正常工作
圈子(弧)(它繼承自QGraphicsItem)的一些代碼。寬度是筆的寬度,所以碰撞位於弧線的外部邊界上。
#include ball.h
QRectF Circle::boundingRect() const
{
QRectF rect(-radius, -radius, radius*2, radius*2);
return rect;
}
QPainterPath Circle::shape() const
{
QPainterPath path;
path.addEllipse(QRectF(-radius-width, -radius-width, (radius+width)*2, (radius+width)*2));
return path;
}
一些代碼從球(它從QGraphicsObject inheits)和碰撞檢測在paint()中。
QPainterPath Ball::shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
QRectF Ball::boundingRect() const
{
QRectF rect(-radius, -radius, radius*2, radius*2);
return rect;
}
void Ball::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if (!scene()->collidingItems(this).isEmpty()) {
for (int y = 0; y<collidingItems().size(); y++)
qDebug() << collidingItems().at(y);
delete this;
}
}
我大概知道錯誤在哪裏:在Circle :: shape()中,路徑是一個橢圓,但它應該是一個弧。我該如何做出這樣的路徑:
painter->drawArc(boundingRect(), startAngle, spanAngle);
它沒有任何想法,我也有一個形狀(),所以邊界框是可以的。當球擊中弧線時,它告訴我它擊中了3弧線,而不是一條線,全部是pos = pos = 1280,540(屏幕中心)。 –