remove some unnecessary CONFIG additions
[profile/ivi/qtdeclarative.git] / src / particles / qquickpointattractor.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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