2016-11-30 8 views
0

私たちのプロジェクトの主題は、フュージョンをシミュレートするプログラムを作っています。複雑なフォームに境界矩形?

クラッセフュージョンとの衝突には問題があります。我々は、衝突のための複雑な形状を作りたいと思う。

printScreenFusionProgramm

これは

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)); 
} 

答えて

0

当社フュージョンクラスである私達の形状が互いに近い2つの円であり、我々は、境界矩形を持っていますが、「複雑な」形にしたいいけない...

オブジェクトの実際の形状を返すには、グラフィックスアイテムの「シェイプ」メソッドを再実装する必要があります。 QPainterPathで必要な形状を返すことができ、Qtは衝突検出にQtを使用します。

+0

こんにちは、ありがとうございました!しかしこれで私たちはスクリーンバグのように見える - > [bugScreen](http://i.imgur.com/P5ib3lL.png)です。 Idkの場合、その形状()は...このバグのための任意のアイデアを持っているので? –

+0

画像は、boundingRectまたは形状関数のいずれかが正確でないか、またはそれらのメソッドの結果に影響を与える操作の前にprepareGeometryChangeの呼び出しに失敗したことを示します。それが起こると、古い領域が適切にクリアされないため、描画アーティファクトが表示されます。 – goug

関連する問題