沿 2D 路径点移动对象

发布于 2024-10-06 03:18:09 字数 1113 浏览 6 评论 0原文

我创建了这个函数来沿着保存在 list_ 中的路径点移动单位。每个单元都有自己的list_move() 最初以每一步的速度(距离/步数)调用。然后根据到下一个路点的距离,采取三种可能的动作。

您能提出任何改进建议吗?

void Unit::move(qreal maxDistance)
{
 // Construct a line that goes from current position to next waypoint
 QLineF line = QLineF(pos(), list_.firstElement().toPointF());

 // Calculate the part of this line that can be "walked" during this step.
 qreal part = maxDistance / line.length();

 // This step's distance is exactly the distance to next waypoint.
 if (part == 1) {
  moveBy(line.dx(), line.dy());
  path_.removeFirst();
 }
 // This step's distance is bigger than the distance to the next waypoint.
 // So we can continue from next waypoint in this step.
 else if (part > 1)
 {
  moveBy(line.dx() , line.dy());
  path_.removeFirst();
  if (!path_.isEmpty())
  {
   move(maxDistance - line.length());
  }
 }
 // This step's distance is not enough to reach next waypoint.
 // Walk the appropriate part of the length.
 else /* part < 1 */
 {
  moveBy(line.dx() * part, line.dy() * part);
 }
}

I created this function to move a unit along way points that are saved in list_. Every Unit has its own list_. move() is initially called with the speed (distance/step) every step. Then depending on the distance to the next way point three possible actions are taken.

Can you suggest any improvements?

void Unit::move(qreal maxDistance)
{
 // Construct a line that goes from current position to next waypoint
 QLineF line = QLineF(pos(), list_.firstElement().toPointF());

 // Calculate the part of this line that can be "walked" during this step.
 qreal part = maxDistance / line.length();

 // This step's distance is exactly the distance to next waypoint.
 if (part == 1) {
  moveBy(line.dx(), line.dy());
  path_.removeFirst();
 }
 // This step's distance is bigger than the distance to the next waypoint.
 // So we can continue from next waypoint in this step.
 else if (part > 1)
 {
  moveBy(line.dx() , line.dy());
  path_.removeFirst();
  if (!path_.isEmpty())
  {
   move(maxDistance - line.length());
  }
 }
 // This step's distance is not enough to reach next waypoint.
 // Walk the appropriate part of the length.
 else /* part < 1 */
 {
  moveBy(line.dx() * part, line.dy() * part);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-10-13 03:18:09

我会讨厌自己建议一种已弃用的做事方式,但没有提及替换方法:(

QGraphicsItemAnimation

为了方便起见,它具有 addStep 和线性插值功能,

似乎 Qt 开发人员希望您使用 QTimeLine 本身作为替代品。

I'll hate myself for suggesting a deprecated way of doing things, but there's no reference to the replacing method :(

QGraphicsItemAnimation

It has addStep and linear interpolation stuff as a convenience.

It seems Qt devs would like you to use QTimeLine itself as a replacement.

空袭的梦i 2024-10-13 03:18:09

我会使用Qt动画框架,更准确地说QPropertyAnimation

// I use QPainterPath to calculate the % of whole route at each waypoint. 
QVector<qreal> lengths;

QPainterPath path;
path.moveTo(list_.first());
lengths.append(0);

foreach (const QPointF &waypoint, list_.mid(1)) {
    path.lineTo(waypoint);
    lengths.append(path.length());
}

// KeyValues is typedef for QVector< QPair<qreal, QVariant> >
KeyValues animationKeyValues; 
for (int i(0); i != lenghts.count(); ++i) {
    animationKeyValues.append(qMakePair(path.percentAtLength(lenghts.at(i)), list_.at(i)));
}

// I assume unit is a pointer to a QObject deriving Unit instance and that
// Unit has QPointF "position" property 
QPropertyAnimation unitAnimation(unit, "position");
unitAnimation.setKeyValues(animationKeyValues);
unitAnimation.setDuration(/* enter desired number here */);
unitAnimation.start();

我还没有测试过这个解决方案,但您应该了解总体思路。

I'd use Qt Animation Framework, more precisely QPropertyAnimation:

// I use QPainterPath to calculate the % of whole route at each waypoint. 
QVector<qreal> lengths;

QPainterPath path;
path.moveTo(list_.first());
lengths.append(0);

foreach (const QPointF &waypoint, list_.mid(1)) {
    path.lineTo(waypoint);
    lengths.append(path.length());
}

// KeyValues is typedef for QVector< QPair<qreal, QVariant> >
KeyValues animationKeyValues; 
for (int i(0); i != lenghts.count(); ++i) {
    animationKeyValues.append(qMakePair(path.percentAtLength(lenghts.at(i)), list_.at(i)));
}

// I assume unit is a pointer to a QObject deriving Unit instance and that
// Unit has QPointF "position" property 
QPropertyAnimation unitAnimation(unit, "position");
unitAnimation.setKeyValues(animationKeyValues);
unitAnimation.setDuration(/* enter desired number here */);
unitAnimation.start();

I haven't tested this solution, but you should get the general idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文