2010-12-06 34 views
1

J'ai créé cette fonction pour déplacer une unité le long des points de route enregistrés dans list_. Chaque unité a son propre list_. move() est initialement appelé avec la vitesse (distance/étape) chaque étape. Ensuite, en fonction de la distance jusqu'au prochain point, trois actions possibles sont prises.Déplacer un objet le long des waypoints en 2D

Pouvez-vous suggérer des améliorations?

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); 
} 
} 
+1

Si vous essayez d'animer des objets, le nouveau cadre d'animation peut être intéressant à lire: http://doc.trolltech.com/latest/animation-overview.html –

Répondre

1

Je me déteste pour suggérer une façon désapprouvée de faire les choses, mais il n'y a aucune référence à la méthode remplaçant :(

QGraphicsItemAnimation

Il a des choses addStep et interpolation linéaire en commodité.

Il semble devs Qt voudrais que vous utilisez QTimeLine lui-même comme un remplacement.

1

J'utilise Qt cadre d'animation, plus précisément 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(); 

Je ne l'ai pas testé cette solution, mais vous devriez obtenir l'idée générale.

+0

J'ai également besoin de traiter la position de l'unité pour la collision , dommages et autres choses à chaque étape et à partir du moment où le chemin est modifié. Est-ce que QPropertyAnimation me donne accès à ceci ou exécute-t-il simplement l'animation une fois qu'elle a été démarrée? Mes unités sont également des sous-classes de QGraphicsItem. Je ne suis pas sûr que QPropertyAnimation fonctionne avec QGraphicsScene. Qu'est-ce que tu penses? – problemofficer

+0

QPropertyAnimation modifie simplement les propriétés de QObject. Si l'une de ces propriétés représente la position sur QGraphicsScene, tout fonctionnera correctement. Je recommande d'utiliser QGraphicsObject au lieu de QGraphicsItem pour les objets animés. La partie collision/dégâts est un problème si vous devez changer le chemin des objets animés après une collision. Sinon, je pense que vous pouvez vérifier tout ce que vous voulez dans le slot connecté au signal QPropertyAnimation :: valueChanged(). – chalup