9d1cb102b9f42250f1ae718de15ce7c50b8201f0
[profile/ivi/qtdeclarative.git] / src / particles / qquickpointattractor.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 "qquickpointattractor_p.h"
43 #include <cmath>
44 #include <QDebug>
45 QT_BEGIN_NAMESPACE
46 /*!
47     \qmlclass Attractor QQuickAttractorAffector
48     \inqmlmodule QtQuick.Particles 2
49     \ingroup qtquick-particles
50     \inherits Affector
51     \brief For attracting particles towards a specific point
52
53     Note that the size and position of this element affects which particles it affects.
54     The size of the point attracted to is always 0x0, and the location of that point
55     is specified by the pointX and pointY properties.
56
57     Note that Attractor has the standard Item x,y,width and height properties.
58     Like other affectors, these represent the affected area. They
59     do not represent the 0x0 point which is the target of the attraction.
60 */
61
62
63 /*!
64     \qmlproperty real QtQuick.Particles2::PointAttractor::pointX
65
66     The x coordinate of the attracting point. This is relative
67     to the x coordinate of the Attractor.
68 */
69 /*!
70     \qmlproperty real QtQuick.Particles2::PointAttractor::pointY
71
72     The y coordinate of the attracting point. This is relative
73     to the y coordinate of the Attractor.
74 */
75 /*!
76     \qmlproperty real QtQuick.Particles2::PointAttractor::strength
77
78     The pull, in units per second, to be exerted on an item one pixel away.
79
80     Depending on how the attraction is proportionalToDistance this may have to
81     be very high or very low to have a reasonable effect on particles at a
82     distance.
83 */
84 /*!
85     \qmlproperty AffectableParameter QtQuick.Particles2::Attractor::affectedParameter
86
87     What attribute of particles is directly affected.
88     \list
89     \li Attractor.Position
90     \li Attractor.Velocity
91     \li Attractor.Acceleration
92     \endlist
93 */
94 /*!
95     \qmlproperty Proportion QtQuick.Particles2::Attractor::proportionalToDistance
96
97     How the distance from the particle to the point affects the strength of the attraction.
98
99     \list
100     \li Attractor.Constant
101     \li Attractor.Linear
102     \li Attractor.InverseLinear
103     \li Attractor.Quadratic
104     \li Attractor.InverseQuadratic
105     \endlist
106 */
107
108
109 QQuickAttractorAffector::QQuickAttractorAffector(QQuickItem *parent) :
110     QQuickParticleAffector(parent), m_strength(0.0), m_x(0), m_y(0)
111   , m_physics(Velocity), m_proportionalToDistance(Linear)
112 {
113 }
114
115 bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
116 {
117     if (m_strength == 0.0)
118         return false;
119     qreal dx = m_x+m_offset.x() - d->curX();
120     qreal dy = m_y+m_offset.y() - d->curY();
121     qreal r = sqrt((dx*dx) + (dy*dy));
122     qreal theta = atan2(dy,dx);
123     qreal ds = 0;
124     switch (m_proportionalToDistance){
125     case InverseQuadratic:
126         ds = (m_strength / qMax<qreal>(1.,r*r));
127         break;
128     case InverseLinear:
129         ds = (m_strength / qMax<qreal>(1.,r));
130         break;
131     case Quadratic:
132         ds = (m_strength * qMax<qreal>(1.,r*r));
133         break;
134     case Linear:
135         ds = (m_strength * qMax<qreal>(1.,r));
136         break;
137     default: //also Constant
138         ds = m_strength;
139     }
140     ds *= dt;
141     dx = ds * cos(theta);
142     dy = ds * sin(theta);
143     qreal vx,vy;
144     switch (m_physics){
145     case Position:
146         d->x = (d->x + dx);
147         d->y = (d->y + dy);
148         break;
149     case Acceleration:
150         d->setInstantaneousAX(d->ax + dx);
151         d->setInstantaneousAY(d->ay + dy);
152         break;
153     case Velocity: //also default
154     default:
155         vx = d->curVX();
156         vy = d->curVY();
157         d->setInstantaneousVX(vx + dx);
158         d->setInstantaneousVY(vy + dy);
159     }
160
161     return true;
162 }
163 QT_END_NAMESPACE