1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickpointattractor_p.h"
41#include <cmath>
42#include <QDebug>
43QT_BEGIN_NAMESPACE
44/*!
45 \qmltype Attractor
46 \instantiates QQuickAttractorAffector
47 \inqmlmodule QtQuick.Particles
48 \ingroup qtquick-particles
49 \inherits Affector
50 \brief For attracting particles towards a specific point.
51
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.
55
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.
59*/
60
61
62/*!
63 \qmlproperty real QtQuick.Particles::PointAttractor::pointX
64
65 The x coordinate of the attracting point. This is relative
66 to the x coordinate of the Attractor.
67*/
68/*!
69 \qmlproperty real QtQuick.Particles::PointAttractor::pointY
70
71 The y coordinate of the attracting point. This is relative
72 to the y coordinate of the Attractor.
73*/
74/*!
75 \qmlproperty real QtQuick.Particles::PointAttractor::strength
76
77 The pull, in units per second, to be exerted on an item one pixel away.
78
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
81 distance.
82*/
83/*!
84 \qmlproperty AffectableParameter QtQuick.Particles::Attractor::affectedParameter
85
86 What attribute of particles is directly affected.
87 \list
88 \li Attractor.Position
89 \li Attractor.Velocity
90 \li Attractor.Acceleration
91 \endlist
92*/
93/*!
94 \qmlproperty Proportion QtQuick.Particles::Attractor::proportionalToDistance
95
96 How the distance from the particle to the point affects the strength of the attraction.
97
98 \list
99 \li Attractor.Constant
100 \li Attractor.Linear
101 \li Attractor.InverseLinear
102 \li Attractor.Quadratic
103 \li Attractor.InverseQuadratic
104 \endlist
105*/
106
107
108QQuickAttractorAffector::QQuickAttractorAffector(QQuickItem *parent) :
109 QQuickParticleAffector(parent), m_strength(0.0), m_x(0), m_y(0)
110 , m_physics(Velocity), m_proportionalToDistance(Linear)
111{
112}
113
114bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
115{
116 if (m_strength == 0.0)
117 return false;
118 qreal dx = m_x+m_offset.x() - d->curX(particleSystem: m_system);
119 qreal dy = m_y+m_offset.y() - d->curY(particleSystem: m_system);
120 qreal r = std::sqrt(x: (dx*dx) + (dy*dy));
121 qreal theta = std::atan2(y: dy,x: dx);
122 qreal ds = 0;
123 switch (m_proportionalToDistance){
124 case InverseQuadratic:
125 ds = (m_strength / qMax<qreal>(a: 1.,b: r*r));
126 break;
127 case InverseLinear:
128 ds = (m_strength / qMax<qreal>(a: 1.,b: r));
129 break;
130 case Quadratic:
131 ds = (m_strength * qMax<qreal>(a: 1.,b: r*r));
132 break;
133 case Linear:
134 ds = (m_strength * qMax<qreal>(a: 1.,b: r));
135 break;
136 default: //also Constant
137 ds = m_strength;
138 }
139 ds *= dt;
140 dx = ds * std::cos(x: theta);
141 dy = ds * std::sin(x: theta);
142 qreal vx,vy;
143 switch (m_physics){
144 case Position:
145 d->x = (d->x + dx);
146 d->y = (d->y + dy);
147 break;
148 case Acceleration:
149 d->setInstantaneousAX(ax: d->ax + dx, particleSystem: m_system);
150 d->setInstantaneousAY(ay: d->ay + dy, particleSystem: m_system);
151 break;
152 case Velocity: //also default
153 default:
154 vx = d->curVX(particleSystem: m_system);
155 vy = d->curVY(particleSystem: m_system);
156 d->setInstantaneousVX(vx: vx + dx, particleSystem: m_system);
157 d->setInstantaneousVY(vy: vy + dy, particleSystem: m_system);
158 }
159
160 return true;
161}
162QT_END_NAMESPACE
163
164#include "moc_qquickpointattractor_p.cpp"
165

source code of qtdeclarative/src/particles/qquickpointattractor.cpp