9697753b421cd3e4b7e435a2a43ebcd39a6caffa
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickpointattractor.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickpointattractor_p.h"
43 #include <cmath>
44 #include <QDebug>
45 QT_BEGIN_NAMESPACE
46 /*!
47     \qmlclass Attractor QQuickAttractorAffector
48     \inqmlmodule QtQuick.Particles 2
49     \inherits Affector
50     \brief The Attractor allows you to attract particles towards a specific point.
51
52     Note that the size and position of this element affects which particles it affects.
53     The size of the point attracted to is always 0x0, and the location of that point
54     is specified by the pointX and pointY properties.
55
56     Note that Attractor has the standard Item x,y,width and height properties.
57     Like other affectors, these represent the affected area. They
58     do not represent the 0x0 point which is the target of the attraction.
59 */
60
61
62 /*!
63     \qmlproperty real QtQuick.Particles2::PointAttractor::pointX
64
65     The x coordinate of the attracting point. This is relative
66     to the x coordinate of the Attractor.
67 */
68 /*!
69     \qmlproperty real QtQuick.Particles2::PointAttractor::pointY
70
71     The y coordinate of the attracting point. This is relative
72     to the y coordinate of the Attractor.
73 */
74 /*!
75     \qmlproperty real QtQuick.Particles2::PointAttractor::strength
76
77     The pull, in units per second, to be exerted on an item one pixel away.
78
79     Depending on how the attraction is proportionalToDistance this may have to
80     be very high or very low to have a reasonable effect on particles at a
81     distance.
82 */
83 /*!
84     \qmlproperty AffectableParameter QtQuick.Particles2::Attractor::affectedParameter
85
86     What attribute of particles is directly affected.
87     \list
88     \o Attractor.Position
89     \o Attractor.Velocity
90     \o Attractor.Acceleration
91     \endlist
92 */
93 /*!
94     \qmlproperty Proportion QtQuick.Particles2::Attractor::proportionalToDistance
95
96     How the distance from the particle to the point affects the strength of the attraction.
97
98     \list
99     \o Attractor.Constant
100     \o Attractor.Linear
101     \o Attractor.InverseLinear
102     \o Attractor.Quadratic
103     \o Attractor.InverseQuadratic
104     \endlist
105 */
106
107
108 QQuickAttractorAffector::QQuickAttractorAffector(QQuickItem *parent) :
109     QQuickParticleAffector(parent), m_strength(0.0), m_x(0), m_y(0)
110   , m_physics(Velocity), m_proportionalToDistance(Linear)
111 {
112 }
113
114 bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
115 {
116     if (m_strength == 0.0)
117         return false;
118     qreal dx = m_x+m_offset.x() - d->curX();
119     qreal dy = m_y+m_offset.y() - d->curY();
120     qreal r = sqrt((dx*dx) + (dy*dy));
121     qreal theta = atan2(dy,dx);
122     qreal ds = 0;
123     switch (m_proportionalToDistance){
124     case InverseQuadratic:
125         ds = (m_strength / qMax<qreal>(1.,r*r));
126         break;
127     case InverseLinear:
128         ds = (m_strength / qMax<qreal>(1.,r));
129         break;
130     case Quadratic:
131         ds = (m_strength * qMax<qreal>(1.,r*r));
132         break;
133     case Linear:
134         ds = (m_strength * qMax<qreal>(1.,r));
135         break;
136     default: //also Constant
137         ds = m_strength;
138     }
139     ds *= dt;
140     dx = ds * cos(theta);
141     dy = ds * sin(theta);
142     qreal vx,vy;
143     switch (m_physics){
144     case Position:
145         d->x = (d->x + dx);
146         d->y = (d->y + dy);
147         break;
148     case Acceleration:
149         d->setInstantaneousAX(d->ax + dx);
150         d->setInstantaneousAY(d->ay + dy);
151         break;
152     case Velocity: //also default
153     default:
154         vx = d->curVX();
155         vy = d->curVY();
156         d->setInstantaneousVX(vx + dx);
157         d->setInstantaneousVY(vy + dy);
158     }
159
160     return true;
161 }
162 QT_END_NAMESPACE