2017-01-24 147 views
0

我知道這可能是一個愚蠢的問題,但我真的似乎無法在任何地方找到答案。我創建了一個這樣的三角形:Qt即時旋轉動畫

QPolygonF triangle; 

    triangle.append(QPointF(0., -15)); 
    triangle.append(QPointF(30., 0)); 
    triangle.append(QPointF(0., 15)); 
    triangle.append(QPointF(15., 0)); 

這個三角形應該代表我的地圖上的一輛汽車,我需要爲它製作動畫。所以,我做了以下內容:

QGraphicsItemAnimation *animation; 
    QGraphicsPolygonItem *clientCar; 
    QTimeLine *timer; 

    animation = new QGraphicsItemAnimation; 

    timer = new QTimeLine(10000); 
    timer->setFrameRange(0, 100); 

    clientCar = scene->addPolygon(triangle, myPen, myBrush) 

    animation->setItem(clientCar); 
    animation->setTimeLine(10000); 

    animation->setPosAt(0.f/200.f, map.street1); 
    animation->setRotationAt(10.f/200.f, 90.f); 
    animation->setPosAt(10.f/200.f, map.street2); 
    animation->setRotationAt(20.f/200.f, 180.f); 
    animation->setPosAt(20.f/200.f, map.street3); 

    scene->addItem(clientCar); 
    ui->graphicsView->setScene(scene); 
    timer->start(); 

的問題是,當它達到一個路口(道路交叉)應轉動,使得其將面臨它會旁邊的道路。正如你在上面看到的,我嘗試過使用setRotationAt(),但它做的是在交叉點之間緩慢旋轉,直到它到達下一個交點。只有當它改變方向時,它纔會立即轉向。任何幫助?

回答

0

從DOC:

QGraphicsItemAnimation將做一個簡單的線性插值 之間的最鄰近的預定的變動來計算矩陣。對於 實例,如果您將項目的位置設置爲值0.0和1.0, ,則動畫將顯示這些項目在 這些位置之間以直線移動的項目。縮放和旋轉也是如此。

線性插值部分將做的伎倆。 那麼你爲什麼不試試這個:

//animation->setPosAt(0.f/200.f, map.street1); 
//animation->setRotationAt(10.f/200.f, 90.f); 
//animation->setPosAt(10.f/200.f, map.street2); 
//animation->setRotationAt(20.f/200.f, 180.f); 
//animation->setPosAt(20.f/200.f, map.street3); 

static float const eps = 1.f/200.f; 
QVector<float> steps = {0.f, 10.f/200.f, 20.f/200.f}; 
QVector<QPointF> points = {map.street1, map.street2, map.street3}; 
QVector<float> angles = {0, 90.f, 180.f}; 

// initial conditions 
animation->setPosAt(steps[0], points[0]); 
animation->setRotationAt(steps[0], angles[0]); 

// for each intersection 
for(size_t inters = 1; inters < points.size(); ++inters) 
{ 
    animation->setRotationAt(steps[inters] - eps, angles[inters - 1]); 
    animation->setPosAt(steps[inters], points[inters]); 
    animation->setRotationAt(steps[inters] + eps, angles[inters]); 
} 
+0

它的工作!非常感謝你! –

+0

@ R.Andrei如果您不介意,請接受我的回答;) – 0Tech