我創建了這個功能來沿着保存在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