aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2011-10-20 21:01:28 +1000
committerQt by Nokia <qt-info@nokia.com>2011-10-24 12:36:29 +0200
commit89fb0fb65d0071e88f24b8022eb7070956a7a1a7 (patch)
tree618a7c6dd18b692c64c1ff6d6d3d91e0c3979176
parenta2b1401b8cacc8fc7a23e17deb5f4e9f4bd34f80 (diff)
Add threshold property to Friction
So much easier than calculating exact counterbalancing acceleration. Change-Id: I7cc43cc2e4d0ad39d1996d432f0e29ccf7bff554 Reviewed-by: Martin Jones <martin.jones@nokia.com>
-rw-r--r--src/declarative/particles/qsgfriction.cpp37
-rw-r--r--src/declarative/particles/qsgfriction_p.h32
-rw-r--r--tests/auto/particles/qsgfriction/data/threshold.qml73
-rw-r--r--tests/auto/particles/qsgfriction/tst_qsgfriction.cpp27
4 files changed, 156 insertions, 13 deletions
diff --git a/src/declarative/particles/qsgfriction.cpp b/src/declarative/particles/qsgfriction.cpp
index 85a586b51c..9e32fc2c3e 100644
--- a/src/declarative/particles/qsgfriction.cpp
+++ b/src/declarative/particles/qsgfriction.cpp
@@ -54,13 +54,23 @@ QT_BEGIN_NAMESPACE
A drag will be applied to moving objects which is this factor of their current velocity.
*/
+/*!
+ \qmlproperty real QtQuick.Particles2::Friction::threshold
+
+ The drag will only be applied to objects with a velocity above the threshold velocity. The
+ drag applied will bring objects down to the threshold velocity, but no further.
+
+ The default threshold is 0
+*/
static qreal sign(qreal a)
{
return a >= 0 ? 1 : -1;
}
+static const qreal epsilon = 0.00001;
+
QSGFrictionAffector::QSGFrictionAffector(QQuickItem *parent) :
- QSGParticleAffector(parent), m_factor(0.0)
+ QSGParticleAffector(parent), m_factor(0.0), m_threshold(0.0)
{
}
@@ -70,14 +80,29 @@ bool QSGFrictionAffector::affectParticle(QSGParticleData *d, qreal dt)
return false;
qreal curVX = d->curVX();
qreal curVY = d->curVY();
+ if (!curVX && !curVY)
+ return false;
qreal newVX = curVX + (curVX * m_factor * -1 * dt);
qreal newVY = curVY + (curVY * m_factor * -1 * dt);
- //Since we're modelling a continuous function, it will never pass 0.
- if (sign(curVX) != sign(newVX))
- newVX = 0;
- if (sign(curVY) != sign(newVY))
- newVY = 0;
+ if (!m_threshold) {
+ if (sign(curVX) != sign(newVX))
+ newVX = 0;
+ if (sign(curVY) != sign(newVY))
+ newVY = 0;
+ } else {
+ qreal curMag = sqrt(curVX*curVX + curVY*curVY);
+ if (curMag <= m_threshold + epsilon)
+ return false;
+ qreal newMag = sqrt(newVX*newVX + newVY*newVY);
+ if (newMag <= m_threshold + epsilon || //went past the threshold, stop there instead
+ sign(curVX) != sign(newVX) || //went so far past maybe it came out the other side!
+ sign(curVY) != sign(newVY)) {
+ qreal theta = atan2(curVY, curVX);
+ newVX = m_threshold * cos(theta);
+ newVY = m_threshold * sin(theta);
+ }
+ }
d->setInstantaneousVX(newVX);
d->setInstantaneousVY(newVY);
diff --git a/src/declarative/particles/qsgfriction_p.h b/src/declarative/particles/qsgfriction_p.h
index e2b3a0c26d..9965b147aa 100644
--- a/src/declarative/particles/qsgfriction_p.h
+++ b/src/declarative/particles/qsgfriction_p.h
@@ -54,6 +54,7 @@ class QSGFrictionAffector : public QSGParticleAffector
{
Q_OBJECT
Q_PROPERTY(qreal factor READ factor WRITE setFactor NOTIFY factorChanged)
+ Q_PROPERTY(qreal threshold READ threshold WRITE setThreshold NOTIFY thresholdChanged)
public:
explicit QSGFrictionAffector(QQuickItem *parent = 0);
@@ -61,24 +62,41 @@ public:
{
return m_factor;
}
+
+ qreal threshold() const
+ {
+ return m_threshold;
+ }
+
protected:
virtual bool affectParticle(QSGParticleData *d, qreal dt);
+
signals:
void factorChanged(qreal arg);
+ void thresholdChanged(qreal arg);
public slots:
-void setFactor(qreal arg)
-{
- if (m_factor != arg) {
- m_factor = arg;
- emit factorChanged(arg);
+ void setFactor(qreal arg)
+ {
+ if (m_factor != arg) {
+ m_factor = arg;
+ emit factorChanged(arg);
+ }
+ }
+
+ void setThreshold(qreal arg)
+ {
+ if (m_threshold != arg) {
+ m_threshold = arg;
+ emit thresholdChanged(arg);
+ }
}
-}
private:
-qreal m_factor;
+ qreal m_factor;
+ qreal m_threshold;
};
QT_END_NAMESPACE
diff --git a/tests/auto/particles/qsgfriction/data/threshold.qml b/tests/auto/particles/qsgfriction/data/threshold.qml
new file mode 100644
index 0000000000..27990c3e7e
--- /dev/null
+++ b/tests/auto/particles/qsgfriction/data/threshold.qml
@@ -0,0 +1,73 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtQuick.Particles 2.0
+
+Rectangle {
+ color: "black"
+ width: 320
+ height: 320
+
+ ParticleSystem {
+ id: sys
+ objectName: "system"
+ anchors.fill: parent
+
+ ImageParticle {
+ groups: ["","notdefault"]
+ source: "../../shared/star.png"
+ }
+
+ Friction {
+ factor: 1000 //speed limit 50
+ threshold: 50
+ }
+
+ Emitter{
+ //0,0 position
+ speed: PointDirection{x:1000}
+ size: 32
+ emitRate: 1000
+ lifeSpan: 500
+ }
+ }
+}
diff --git a/tests/auto/particles/qsgfriction/tst_qsgfriction.cpp b/tests/auto/particles/qsgfriction/tst_qsgfriction.cpp
index ad37c3ef38..3237c92421 100644
--- a/tests/auto/particles/qsgfriction/tst_qsgfriction.cpp
+++ b/tests/auto/particles/qsgfriction/tst_qsgfriction.cpp
@@ -52,6 +52,7 @@ public:
private slots:
void test_basic();
+ void test_threshold();
};
tst_qsgfriction::tst_qsgfriction()
@@ -102,6 +103,32 @@ void tst_qsgfriction::test_basic()
}
}
+void tst_qsgfriction::test_threshold()
+{
+ QQuickView* view = createView(QCoreApplication::applicationDirPath() + "/data/threshold.qml", 600);
+ QSGParticleSystem* system = view->rootObject()->findChild<QSGParticleSystem*>("system");
+ ensureAnimTime(600, system->m_animation);
+
+ //Speed capped at 50, but it might take a frame or two to get there
+ QCOMPARE(system->groupData[0]->size(), 500);
+ foreach (QSGParticleData *d, system->groupData[0]->data) {
+ if (d->t == -1.0f)
+ continue; //Particle data unused
+ if (myFuzzyGEQ(d->t, ((qreal)system->timeInt/1000.0) - 0.1))
+ continue; //Particle data too young
+
+ QVERIFY(myFuzzyLEQ(d->vx, 50.f));
+ QCOMPARE(d->y, 0.f);
+ QCOMPARE(d->vy, 0.f);
+ QCOMPARE(d->ax, 0.f);
+ QCOMPARE(d->ay, 0.f);
+ QCOMPARE(d->lifeSpan, 0.5f);
+ QCOMPARE(d->size, 32.f);
+ QCOMPARE(d->endSize, 32.f);
+ QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
+ }
+}
+
QTEST_MAIN(tst_qsgfriction);
#include "tst_qsgfriction.moc"