Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickwander.cpp
Go to the documentation of this file.
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>
48 QQuickParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
49 , m_affectedParameter(Velocity)
50{
51 m_needsReset = true;
52}
53
55{
57 iter != m_wanderData.constEnd(); ++iter)
58 delete (*iter);
59}
60
61WanderData* QQuickWanderAffector::getData(int idx)
62{
63 if (m_wanderData.contains(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(idx, 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
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(m_system) + dx;
120 if (m_xVariance > qAbs(newX) )
121 data->x += dx;
122 newY = data->curY(m_system) + dy;
123 if (m_yVariance > qAbs(newY) )
124 data->y += dy;
125 break;
126 default:
127 case Velocity:
128 newX = data->curVX(m_system) + dx;
129 if (m_xVariance > qAbs(newX))
130 data->setInstantaneousVX(newX, m_system);
131 newY = data->curVY(m_system) + dy;
132 if (m_yVariance > qAbs(newY))
133 data->setInstantaneousVY(newY, m_system);
134 break;
135 case Acceleration:
136 newX = data->ax + dx;
137 if (m_xVariance > qAbs(newX))
138 data->setInstantaneousAX(newX, m_system);
139 newY = data->ay + dy;
140 if (m_yVariance > qAbs(newY))
141 data->setInstantaneousAY(newY, m_system);
142 break;
143 }
144 return true;
145}
147
148#include "moc_qquickwander_p.cpp"
\inmodule QtCore
Definition qhash.h:1145
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
QQuickParticleSystem * m_system
QQuickWanderAffector(QQuickItem *parent=nullptr)
\qmltype Wander \instantiates QQuickWanderAffector \inqmlmodule QtQuick.Particles\inherits Affector
bool affectParticle(QQuickParticleData *d, qreal dt) override
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
double qreal
Definition qtypes.h:187