Rename speed -> velocity in the particle system
[profile/ivi/qtdeclarative.git] / src / particles / qquickfriction.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickfriction_p.h"
43 QT_BEGIN_NAMESPACE
44 /*!
45     \qmlclass Friction QQuickFrictionAffector
46     \inqmlmodule QtQuick.Particles 2
47     \ingroup qtquick-particles
48     \inherits Affector
49     \brief For applying friction proportional to the particle's current velocity
50
51 */
52
53 /*!
54     \qmlproperty real QtQuick.Particles2::Friction::factor
55
56     A drag will be applied to moving objects which is this factor of their current velocity.
57 */
58 /*!
59     \qmlproperty real QtQuick.Particles2::Friction::threshold
60
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.
63
64     The default threshold is 0
65 */
66 static qreal sign(qreal a)
67 {
68     return a >= 0 ? 1 : -1;
69 }
70
71 static const qreal epsilon = 0.00001;
72
73 QQuickFrictionAffector::QQuickFrictionAffector(QQuickItem *parent) :
74     QQuickParticleAffector(parent), m_factor(0.0), m_threshold(0.0)
75 {
76 }
77
78 bool QQuickFrictionAffector::affectParticle(QQuickParticleData *d, qreal dt)
79 {
80     if (!m_factor)
81         return false;
82     qreal curVX = d->curVX();
83     qreal curVY = d->curVY();
84     if (!curVX && !curVY)
85         return false;
86     qreal newVX = curVX + (curVX * m_factor * -1 * dt);
87     qreal newVY = curVY + (curVY * m_factor * -1 * dt);
88
89     if (!m_threshold) {
90         if (sign(curVX) != sign(newVX))
91             newVX = 0;
92         if (sign(curVY) != sign(newVY))
93             newVY = 0;
94     } else {
95         qreal curMag = sqrt(curVX*curVX + curVY*curVY);
96         if (curMag <= m_threshold + epsilon)
97             return false;
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);
105         }
106     }
107
108     d->setInstantaneousVX(newVX);
109     d->setInstantaneousVY(newVY);
110     return true;
111 }
112 QT_END_NAMESPACE