1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the Declarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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
50 \brief The Attractor allows you to attract particles towards a specific point.
52 Note that the size and position of this element affects which particles it affects.
53 The size of the point attracted to is always 0x0, and the location of that point
54 is specified by the pointX and pointY properties.
56 Note that Attractor has the standard Item x,y,width and height properties.
57 Like other affectors, these represent the affected area. They
58 do not represent the 0x0 point which is the target of the attraction.
63 \qmlproperty real QtQuick.Particles2::PointAttractor::pointX
65 The x coordinate of the attracting point. This is relative
66 to the x coordinate of the Attractor.
69 \qmlproperty real QtQuick.Particles2::PointAttractor::pointY
71 The y coordinate of the attracting point. This is relative
72 to the y coordinate of the Attractor.
75 \qmlproperty real QtQuick.Particles2::PointAttractor::strength
77 The pull, in units per second, to be exerted on an item one pixel away.
79 Depending on how the attraction is proportionalToDistance this may have to
80 be very high or very low to have a reasonable effect on particles at a
84 \qmlproperty AffectableParameter QtQuick.Particles2::Attractor::affectedParameter
86 What attribute of particles is directly affected.
90 \o Attractor.Acceleration
94 \qmlproperty Proportion QtQuick.Particles2::Attractor::proportionalToDistance
96 How the distance from the particle to the point affects the strength of the attraction.
101 \o Attractor.InverseLinear
102 \o Attractor.Quadratic
103 \o Attractor.InverseQuadratic
108 QQuickAttractorAffector::QQuickAttractorAffector(QQuickItem *parent) :
109 QQuickParticleAffector(parent), m_strength(0.0), m_x(0), m_y(0)
110 , m_physics(Velocity), m_proportionalToDistance(Linear)
114 bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
116 if (m_strength == 0.0)
118 qreal dx = m_x+m_offset.x() - d->curX();
119 qreal dy = m_y+m_offset.y() - d->curY();
120 qreal r = sqrt((dx*dx) + (dy*dy));
121 qreal theta = atan2(dy,dx);
123 switch (m_proportionalToDistance){
124 case InverseQuadratic:
125 ds = (m_strength / qMax<qreal>(1.,r*r));
128 ds = (m_strength / qMax<qreal>(1.,r));
131 ds = (m_strength * qMax<qreal>(1.,r*r));
134 ds = (m_strength * qMax<qreal>(1.,r));
136 default: //also Constant
140 dx = ds * cos(theta);
141 dy = ds * sin(theta);
149 d->setInstantaneousAX(d->ax + dx);
150 d->setInstantaneousAY(d->ay + dy);
152 case Velocity: //also default
156 d->setInstantaneousVX(vx + dx);
157 d->setInstantaneousVY(vy + dy);