1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQuick module of the Qt Toolkit.
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.
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.
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.
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.
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);