1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickwander_p.h"
43 #include "qquickparticlesystem_p.h"//for ParticlesVertices
46 \qmlclass Wander QQuickWanderAffector
47 \inqmlmodule QtQuick.Particles 2
48 \ingroup qtquick-particles
50 \brief For applying random particle trajectory
54 \qmlproperty real QtQuick.Particles2::Wander::pace
56 Maximum attribute change per second.
59 \qmlproperty real QtQuick.Particles2::Wander::xVariance
61 Maximum attribute x value (as a result of Wander).
63 If unset, Wander will not affect x values.
66 \qmlproperty real QtQuick.Particles2::Wander::yVariance
68 Maximum attribute y value (as a result of Wander).
70 If unset, Wander will not affect y values.
73 \qmlproperty AffectableParameter QtQuick.Particles2::Wander::affectedParameter
75 What attribute of particles is directly affected.
77 \li PointAttractor.Position
78 \li PointAttractor.Velocity
79 \li PointAttractor.Acceleration
83 QQuickWanderAffector::QQuickWanderAffector(QQuickItem *parent) :
84 QQuickParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
85 , m_affectedParameter(Velocity)
90 QQuickWanderAffector::~QQuickWanderAffector()
92 for (QHash<int, WanderData*>::const_iterator iter=m_wanderData.constBegin();
93 iter != m_wanderData.constEnd(); iter++)
97 WanderData* QQuickWanderAffector::getData(int idx)
99 if (m_wanderData.contains(idx))
100 return m_wanderData[idx];
101 WanderData* d = new WanderData;
104 d->x_peak = m_xVariance;
105 d->y_peak = m_yVariance;
106 d->x_var = m_pace * qreal(qrand()) / RAND_MAX;
107 d->y_var = m_pace * qreal(qrand()) / RAND_MAX;
109 m_wanderData.insert(idx, d);
114 //void QQuickWanderAffector::reset(int systemIdx)
116 // if (m_wanderData.contains(systemIdx))
117 // delete m_wanderData[systemIdx];
118 // m_wanderData.remove(systemIdx);
121 bool QQuickWanderAffector::affectParticle(QQuickParticleData* data, qreal dt)
123 /*TODO: Add a mode which does basically this - picking a direction, going in it (random speed) and then going back
124 WanderData* d = getData(data->systemIndex);
125 if (m_xVariance != 0.) {
126 if ((d->x_vel > d->x_peak && d->x_var > 0.0) || (d->x_vel < -d->x_peak && d->x_var < 0.0)) {
127 d->x_var = -d->x_var;
128 d->x_peak = m_xVariance + m_xVariance * qreal(qrand()) / RAND_MAX;
130 d->x_vel += d->x_var * dt;
132 qreal dx = dt * d->x_vel;
134 if (m_yVariance != 0.) {
135 if ((d->y_vel > d->y_peak && d->y_var > 0.0) || (d->y_vel < -d->y_peak && d->y_var < 0.0)) {
136 d->y_var = -d->y_var;
137 d->y_peak = m_yVariance + m_yVariance * qreal(qrand()) / RAND_MAX;
139 d->y_vel += d->y_var * dt;
141 qreal dy = dt * d->x_vel;
143 //### Should we be amending vel instead?
144 ParticleVertex* p = &(data->pv);
150 qreal dx = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
151 qreal dy = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
153 switch (m_affectedParameter){
155 newX = data->curX() + dx;
156 if (m_xVariance > qAbs(newX) )
158 newY = data->curY() + dy;
159 if (m_yVariance > qAbs(newY) )
164 newX = data->curVX() + dx;
165 if (m_xVariance > qAbs(newX) )
166 data->setInstantaneousVX(newX);
167 newY = data->curVY() + dy;
168 if (m_yVariance > qAbs(newY) )
169 data->setInstantaneousVY(newY);
172 newX = data->ax + dx;
173 if (m_xVariance > qAbs(newX) )
174 data->setInstantaneousAX(newX);
175 newY = data->ay + dy;
176 if (m_yVariance > qAbs(newY) )
177 data->setInstantaneousAY(newY);