我创建了这个功能来沿着保存在list_
中的路点移动一个单元。每个单位都有自己的list_
。最初用每一步的速度(距离/步长)调用move()
。然后根据距下一路点的距离采取三种可能的行动。沿着2D中的路标移动物体
你能提出任何改进建议吗?
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);
}
}
如果您尝试为对象设置动画效果,则新的动画框架可能值得您阅读:http://doc.trolltech.com/latest/animation-overview.html – 2010-12-06 15:00:02