1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qquickpointattractor_p.h"
47 \qmlclass Attractor QQuickAttractorAffector
48 \inqmlmodule QtQuick.Particles 2
49 \ingroup qtquick-particles
51 \brief For attracting particles towards a specific point
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.
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.
64 \qmlproperty real QtQuick.Particles2::PointAttractor::pointX
66 The x coordinate of the attracting point. This is relative
67 to the x coordinate of the Attractor.
70 \qmlproperty real QtQuick.Particles2::PointAttractor::pointY
72 The y coordinate of the attracting point. This is relative
73 to the y coordinate of the Attractor.
76 \qmlproperty real QtQuick.Particles2::PointAttractor::strength
78 The pull, in units per second, to be exerted on an item one pixel away.
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
85 \qmlproperty AffectableParameter QtQuick.Particles2::Attractor::affectedParameter
87 What attribute of particles is directly affected.
89 \li Attractor.Position
90 \li Attractor.Velocity
91 \li Attractor.Acceleration
95 \qmlproperty Proportion QtQuick.Particles2::Attractor::proportionalToDistance
97 How the distance from the particle to the point affects the strength of the attraction.
100 \li Attractor.Constant
102 \li Attractor.InverseLinear
103 \li Attractor.Quadratic
104 \li Attractor.InverseQuadratic
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)
115 bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
117 if (m_strength == 0.0)
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);
124 switch (m_proportionalToDistance){
125 case InverseQuadratic:
126 ds = (m_strength / qMax<qreal>(1.,r*r));
129 ds = (m_strength / qMax<qreal>(1.,r));
132 ds = (m_strength * qMax<qreal>(1.,r*r));
135 ds = (m_strength * qMax<qreal>(1.,r));
137 default: //also Constant
141 dx = ds * cos(theta);
142 dy = ds * sin(theta);
150 d->setInstantaneousAX(d->ax + dx);
151 d->setInstantaneousAY(d->ay + dy);
153 case Velocity: //also default
157 d->setInstantaneousVX(vx + dx);
158 d->setInstantaneousVY(vy + dy);