1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickpointattractor_p.h"
5#include <cmath>
6#include <QDebug>
7QT_BEGIN_NAMESPACE
8/*!
9 \qmltype Attractor
10 \instantiates QQuickAttractorAffector
11 \inqmlmodule QtQuick.Particles
12 \ingroup qtquick-particles
13 \inherits Affector
14 \brief Attracts particles towards a specific point.
15
16 Like other affectors, Attractor has the standard properties x, y, width,
17 and height that represent the affected area. The size and position of the
18 Attractor item determine the affected particles.
19
20 The size of the attracting point is always 0x0, and its location is
21 specified by \l pointX and \l pointY properties.
22*/
23
24
25/*!
26 \qmlproperty real QtQuick.Particles::Attractor::pointX
27
28 The x coordinate of the attracting point, relative
29 to the x coordinate of the Attractor item.
30*/
31/*!
32 \qmlproperty real QtQuick.Particles::Attractor::pointY
33
34 The y coordinate of the attracting point, relative
35 to the y coordinate of the Attractor item.
36*/
37/*!
38 \qmlproperty real QtQuick.Particles::Attractor::strength
39
40 The pull, in units per second, to be exerted on an item one pixel away.
41
42 Strength, together with the value of \l proportionalToDistance property,
43 determine the exact amount of pull exerted on particles at a distance.
44*/
45/*!
46 \qmlproperty enumeration QtQuick.Particles::Attractor::affectedParameter
47
48 The attribute of particles that is directly affected.
49
50 \value Attractor.Position Position
51 \value Attractor.Velocity Velocity
52 \value Attractor.Acceleration Acceleration
53*/
54/*!
55 \qmlproperty enumeration QtQuick.Particles::Attractor::proportionalToDistance
56
57 The relation between the \l strength of the attraction and the distance from
58 the particle to the attracting point.
59
60 \value Attractor.Constant Constant
61 \value Attractor.Linear Linear
62 \value Attractor.InverseLinear Inverse linear
63 \value Attractor.Quadratic Quadratic
64 \value Attractor.InverseQuadratic Inverse quadratic
65*/
66
67
68QQuickAttractorAffector::QQuickAttractorAffector(QQuickItem *parent) :
69 QQuickParticleAffector(parent), m_strength(0.0), m_x(0), m_y(0)
70 , m_physics(Velocity), m_proportionalToDistance(Linear)
71{
72}
73
74bool QQuickAttractorAffector::affectParticle(QQuickParticleData *d, qreal dt)
75{
76 if (m_strength == 0.0)
77 return false;
78 qreal dx = m_x+m_offset.x() - d->curX(particleSystem: m_system);
79 qreal dy = m_y+m_offset.y() - d->curY(particleSystem: m_system);
80 qreal r = std::sqrt(x: (dx*dx) + (dy*dy));
81 qreal theta = std::atan2(y: dy,x: dx);
82 qreal ds = 0;
83 switch (m_proportionalToDistance){
84 case InverseQuadratic:
85 ds = (m_strength / qMax<qreal>(a: 1.,b: r*r));
86 break;
87 case InverseLinear:
88 ds = (m_strength / qMax<qreal>(a: 1.,b: r));
89 break;
90 case Quadratic:
91 ds = (m_strength * qMax<qreal>(a: 1.,b: r*r));
92 break;
93 case Linear:
94 ds = (m_strength * qMax<qreal>(a: 1.,b: r));
95 break;
96 default: //also Constant
97 ds = m_strength;
98 }
99 ds *= dt;
100 dx = ds * std::cos(x: theta);
101 dy = ds * std::sin(x: theta);
102 qreal vx,vy;
103 switch (m_physics){
104 case Position:
105 d->x = (d->x + dx);
106 d->y = (d->y + dy);
107 break;
108 case Acceleration:
109 d->setInstantaneousAX(ax: d->ax + dx, particleSystem: m_system);
110 d->setInstantaneousAY(ay: d->ay + dy, particleSystem: m_system);
111 break;
112 case Velocity: //also default
113 default:
114 vx = d->curVX(particleSystem: m_system);
115 vy = d->curVY(particleSystem: m_system);
116 d->setInstantaneousVX(vx: vx + dx, particleSystem: m_system);
117 d->setInstantaneousVY(vy: vy + dy, particleSystem: m_system);
118 }
119
120 return true;
121}
122QT_END_NAMESPACE
123
124#include "moc_qquickpointattractor_p.cpp"
125

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