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