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"
48 \instantiates QQuickAttractorAffector
49 \inqmlmodule QtQuick.Particles 2
50 \ingroup qtquick-particles
52 \brief For attracting particles towards a specific point
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.
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.
65 \qmlproperty real QtQuick.Particles2::PointAttractor::pointX
67 The x coordinate of the attracting point. This is relative
68 to the x coordinate of the Attractor.
71 \qmlproperty real QtQuick.Particles2::PointAttractor::pointY
73 The y coordinate of the attracting point. This is relative
74 to the y coordinate of the Attractor.
77 \qmlproperty real QtQuick.Particles2::PointAttractor::strength
79 The pull, in units per second, to be exerted on an item one pixel away.
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
86 \qmlproperty AffectableParameter QtQuick.Particles2::Attractor::affectedParameter
88 What attribute of particles is directly affected.
90 \li Attractor.Position
91 \li Attractor.Velocity
92 \li Attractor.Acceleration
96 \qmlproperty Proportion QtQuick.Particles2::Attractor::proportionalToDistance
98 How the distance from the particle to the point affects the strength of the attraction.
101 \li Attractor.Constant
103 \li Attractor.InverseLinear
104 \li Attractor.Quadratic
105 \li Attractor.InverseQuadratic
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)
116 bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
118 if (m_strength == 0.0)
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);
125 switch (m_proportionalToDistance){
126 case InverseQuadratic:
127 ds = (m_strength / qMax<qreal>(1.,r*r));
130 ds = (m_strength / qMax<qreal>(1.,r));
133 ds = (m_strength * qMax<qreal>(1.,r*r));
136 ds = (m_strength * qMax<qreal>(1.,r));
138 default: //also Constant
142 dx = ds * cos(theta);
143 dy = ds * sin(theta);
151 d->setInstantaneousAX(d->ax + dx);
152 d->setInstantaneousAY(d->ay + dy);
154 case Velocity: //also default
158 d->setInstantaneousVX(vx + dx);
159 d->setInstantaneousVY(vy + dy);