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 "qquickwander_p.h"
5#include "qquickparticlesystem_p.h"//for ParticlesVertices
6#include <QRandomGenerator>
7QT_BEGIN_NAMESPACE
8/*!
9 \qmltype Wander
10 \instantiates QQuickWanderAffector
11 \inqmlmodule QtQuick.Particles
12 \ingroup qtquick-particles
13 \inherits Affector
14 \brief For applying random particle trajectory.
15
16*/
17/*!
18 \qmlproperty real QtQuick.Particles::Wander::pace
19
20 Maximum attribute change per second.
21*/
22/*!
23 \qmlproperty real QtQuick.Particles::Wander::xVariance
24
25 Maximum attribute x value (as a result of Wander).
26
27 If unset, Wander will not affect x values.
28*/
29/*!
30 \qmlproperty real QtQuick.Particles::Wander::yVariance
31
32 Maximum attribute y value (as a result of Wander).
33
34 If unset, Wander will not affect y values.
35*/
36/*!
37 \qmlproperty AffectableParameter QtQuick.Particles::Wander::affectedParameter
38
39 What attribute of particles is directly affected.
40 \list
41 \li PointAttractor.Position
42 \li PointAttractor.Velocity
43 \li PointAttractor.Acceleration
44 \endlist
45*/
46
47QQuickWanderAffector::QQuickWanderAffector(QQuickItem *parent) :
48 QQuickParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
49 , m_affectedParameter(Velocity)
50{
51 m_needsReset = true;
52}
53
54QQuickWanderAffector::~QQuickWanderAffector()
55{
56 for (QHash<int, WanderData*>::const_iterator iter=m_wanderData.constBegin();
57 iter != m_wanderData.constEnd(); ++iter)
58 delete (*iter);
59}
60
61WanderData* QQuickWanderAffector::getData(int idx)
62{
63 if (m_wanderData.contains(key: idx))
64 return m_wanderData[idx];
65 WanderData* d = new WanderData;
66 d->x_vel = 0;
67 d->y_vel = 0;
68 d->x_peak = m_xVariance;
69 d->y_peak = m_yVariance;
70 d->x_var = m_pace * QRandomGenerator::global()->generateDouble();
71 d->y_var = m_pace * QRandomGenerator::global()->generateDouble();
72
73 m_wanderData.insert(key: idx, value: d);
74 return d;
75}
76
77// TODO: see below
78//void QQuickWanderAffector::reset(int systemIdx)
79//{
80// if (m_wanderData.contains(systemIdx))
81// delete m_wanderData[systemIdx];
82// m_wanderData.remove(systemIdx);
83//}
84
85bool QQuickWanderAffector::affectParticle(QQuickParticleData* data, qreal dt)
86{
87 /*TODO: Add a mode which does basically this - picking a direction, going in it (random velocity) and then going back
88 WanderData* d = getData(data->systemIndex);
89 if (m_xVariance != 0.) {
90 if ((d->x_vel > d->x_peak && d->x_var > 0.0) || (d->x_vel < -d->x_peak && d->x_var < 0.0)) {
91 d->x_var = -d->x_var;
92 d->x_peak = m_xVariance + m_xVariance * QRandomGenerator::global()->generateDouble();
93 }
94 d->x_vel += d->x_var * dt;
95 }
96 qreal dx = dt * d->x_vel;
97
98 if (m_yVariance != 0.) {
99 if ((d->y_vel > d->y_peak && d->y_var > 0.0) || (d->y_vel < -d->y_peak && d->y_var < 0.0)) {
100 d->y_var = -d->y_var;
101 d->y_peak = m_yVariance + m_yVariance * QRandomGenerator::global()->generateDouble();
102 }
103 d->y_vel += d->y_var * dt;
104 }
105 qreal dy = dt * d->x_vel;
106
107 //### Should we be amending vel instead?
108 ParticleVertex* p = &(data->pv);
109 p->x += dx;
110
111 p->y += dy;
112 return true;
113 */
114 qreal dx = dt * m_pace * (2 * QRandomGenerator::global()->generateDouble() - 1);
115 qreal dy = dt * m_pace * (2 * QRandomGenerator::global()->generateDouble() - 1);
116 qreal newX, newY;
117 switch (m_affectedParameter){
118 case Position:
119 newX = data->curX(particleSystem: m_system) + dx;
120 if (m_xVariance > qAbs(t: newX) )
121 data->x += dx;
122 newY = data->curY(particleSystem: m_system) + dy;
123 if (m_yVariance > qAbs(t: newY) )
124 data->y += dy;
125 break;
126 default:
127 case Velocity:
128 newX = data->curVX(particleSystem: m_system) + dx;
129 if (m_xVariance > qAbs(t: newX))
130 data->setInstantaneousVX(vx: newX, particleSystem: m_system);
131 newY = data->curVY(particleSystem: m_system) + dy;
132 if (m_yVariance > qAbs(t: newY))
133 data->setInstantaneousVY(vy: newY, particleSystem: m_system);
134 break;
135 case Acceleration:
136 newX = data->ax + dx;
137 if (m_xVariance > qAbs(t: newX))
138 data->setInstantaneousAX(ax: newX, particleSystem: m_system);
139 newY = data->ay + dy;
140 if (m_yVariance > qAbs(t: newY))
141 data->setInstantaneousAY(ay: newY, particleSystem: m_system);
142 break;
143 }
144 return true;
145}
146QT_END_NAMESPACE
147
148#include "moc_qquickwander_p.cpp"
149

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