1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickfriction_p.h"
45 \qmlclass Friction QQuickFrictionAffector
46 \inqmlmodule QtQuick.Particles 2
47 \ingroup qtquick-particles
49 \brief For applying friction proportional to the particle's current speed
54 \qmlproperty real QtQuick.Particles2::Friction::factor
56 A drag will be applied to moving objects which is this factor of their current velocity.
59 \qmlproperty real QtQuick.Particles2::Friction::threshold
61 The drag will only be applied to objects with a velocity above the threshold velocity. The
62 drag applied will bring objects down to the threshold velocity, but no further.
64 The default threshold is 0
66 static qreal sign(qreal a)
68 return a >= 0 ? 1 : -1;
71 static const qreal epsilon = 0.00001;
73 QQuickFrictionAffector::QQuickFrictionAffector(QQuickItem *parent) :
74 QQuickParticleAffector(parent), m_factor(0.0), m_threshold(0.0)
78 bool QQuickFrictionAffector::affectParticle(QQuickParticleData *d, qreal dt)
82 qreal curVX = d->curVX();
83 qreal curVY = d->curVY();
86 qreal newVX = curVX + (curVX * m_factor * -1 * dt);
87 qreal newVY = curVY + (curVY * m_factor * -1 * dt);
90 if (sign(curVX) != sign(newVX))
92 if (sign(curVY) != sign(newVY))
95 qreal curMag = sqrt(curVX*curVX + curVY*curVY);
96 if (curMag <= m_threshold + epsilon)
98 qreal newMag = sqrt(newVX*newVX + newVY*newVY);
99 if (newMag <= m_threshold + epsilon || //went past the threshold, stop there instead
100 sign(curVX) != sign(newVX) || //went so far past maybe it came out the other side!
101 sign(curVY) != sign(newVY)) {
102 qreal theta = atan2(curVY, curVX);
103 newVX = m_threshold * cos(theta);
104 newVY = m_threshold * sin(theta);
108 d->setInstantaneousVX(newVX);
109 d->setInstantaneousVY(newVY);