summaryrefslogtreecommitdiffstats
path: root/examples/statemachine
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-05 12:18:55 (GMT)
committerEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-05 12:18:55 (GMT)
commit8b4daee4bd6c6dd3da7b7e71b6c468059cf53ea0 (patch)
tree1982523656186b957528e6149b4b354a3c9f81a6 /examples/statemachine
parentd388730da773c46f228894186c29d330a774b72b (diff)
downloadQt-8b4daee4bd6c6dd3da7b7e71b6c468059cf53ea0.zip
Qt-8b4daee4bd6c6dd3da7b7e71b6c468059cf53ea0.tar.gz
Qt-8b4daee4bd6c6dd3da7b7e71b6c468059cf53ea0.tar.bz2
Make sure the correct position/direction is actually set at the end of a
loop. Also make sure we set the direction to an angle within 360 degrees.
Diffstat (limited to 'examples/statemachine')
-rw-r--r--examples/statemachine/errorstate/tankitem.cpp26
1 files changed, 16 insertions, 10 deletions
diff --git a/examples/statemachine/errorstate/tankitem.cpp b/examples/statemachine/errorstate/tankitem.cpp
index fc71ef9..5506a7e 100644
--- a/examples/statemachine/errorstate/tankitem.cpp
+++ b/examples/statemachine/errorstate/tankitem.cpp
@@ -2,6 +2,7 @@
#include <QPainter>
#include <QGraphicsScene>
+#include <QDebug>
#include <math.h>
@@ -38,11 +39,12 @@ public:
{
qreal dist = timeDelta * item()->speed() * (m_reverse ? -1.0 : 1.0);
+ bool done = false;
+ if (qAbs(m_distance) < qAbs(dist)) {
+ done = true;
+ dist = m_distance;
+ }
m_distance -= dist;
- if (m_reverse && m_distance > 0.0)
- return false;
- else if (!m_reverse && m_distance < 0.0)
- return false;
qreal a = item()->direction() * M_PI / 180.0;
@@ -50,7 +52,7 @@ public:
qreal xd = dist * sin(M_PI / 2.0 - a);
item()->setPos(item()->pos() + QPointF(xd, yd));
- return true;
+ return !done;
}
private:
@@ -70,14 +72,15 @@ public:
bool apply(qreal timeDelta)
{
qreal dist = timeDelta * item()->angularSpeed() * (m_reverse ? -1.0 : 1.0);
+ bool done = false;
+ if (qAbs(m_distance) < qAbs(dist)) {
+ done = true;
+ dist = m_distance;
+ }
m_distance -= dist;
- if (m_reverse && m_distance > 0.0)
- return false;
- else if (!m_reverse && m_distance < 0.0)
- return false;
item()->setDirection(item()->direction() + dist);
- return true;
+ return !done;
}
private:
@@ -219,6 +222,9 @@ qreal TankItem::direction() const
void TankItem::setDirection(qreal newDirection)
{
+ int fullRotations = int(newDirection) / 360;
+ newDirection -= fullRotations * 360.0;
+
qreal diff = newDirection - m_currentDirection;
m_currentDirection = newDirection;
rotate(diff);