Qt立即旋转动画
Qt instantly rotate animation
我知道这可能是一个愚蠢的问题,但我似乎真的无法在任何地方找到答案。我创建了一个这样的三角形:
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(),但它所做的是在交叉点之间缓慢旋转,直到到达下一个交叉点。它应该在瞬间转动,只有当它改变它的方式时。有什么帮助吗?
从文档中:
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]);
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 与互斥锁相比,旋转锁可以保证上下文切换
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 使用 GLUT 使用键停止动画?
- 顺时针迭代旋转 3 位数字
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- ASCII 旋转光标 (TUI) 动画出现问题
- 围绕特定轴对 qt3d 旋转进行动画处理
- C++中船只动画的旋转
- Qt立即旋转动画
- c++中的动画,用旋转矩阵插值关键帧
- Opencv动画使用旋转矩阵旋转对象