1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dparticletargetdirection_p.h"
5#include "qquick3dparticlerandomizer_p.h"
6#include "qquick3dparticlesystem_p.h"
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype TargetDirection3D
12 \inherits Direction3D
13 \inqmlmodule QtQuick3D.Particles3D
14 \brief For specifying a direction towards the target position.
15 \since 6.2
16
17 This element sets emitted particle velocity towards the target position.
18
19 For example, to emit particles towards position (100, 0, 0) with
20 random magnitude between 10..20:
21
22 \qml
23 ParticleEmitter3D {
24 ...
25 velocity: TargetDirection3D {
26 position: Qt.vector3d(100, 0, 0)
27 normalized: true
28 magnitude: 15.0
29 magnitudeVariation: 5.0
30 }
31 }
32 \endqml
33*/
34
35QQuick3DParticleTargetDirection::QQuick3DParticleTargetDirection(QObject *parent)
36 : QQuick3DParticleDirection(parent)
37{
38}
39
40/*!
41 \qmlproperty vector3d TargetDirection3D::position
42
43 This property defines the position for particles target.
44
45 The default value is \c (0, 0, 0) (the center of the emitter).
46
47 \sa positionVariation
48*/
49QVector3D QQuick3DParticleTargetDirection::position() const
50{
51 return m_position;
52}
53
54/*!
55 \qmlproperty vector3d TargetDirection3D::positionVariation
56
57 This property defines the position variation for particles target.
58
59 The default value is \c (0, 0, 0) (no variation).
60
61 \sa position
62*/
63QVector3D QQuick3DParticleTargetDirection::positionVariation() const
64{
65 return m_positionVariation;
66}
67
68void QQuick3DParticleTargetDirection::setPosition(const QVector3D &position)
69{
70 if (m_position == position)
71 return;
72
73 m_position = position;
74 Q_EMIT positionChanged();
75}
76
77void QQuick3DParticleTargetDirection::setPositionVariation(const QVector3D &positionVariation)
78{
79 if (m_positionVariation == positionVariation)
80 return;
81
82 m_positionVariation = positionVariation;
83 Q_EMIT positionVariationChanged();
84}
85
86/*!
87 \qmlproperty bool TargetDirection3D::normalized
88
89 This property defines if the distance to \l position should be considered as normalized or not.
90 When this is false, distance to the \l position affects the magnitude of the particles velocity.
91 When set to true, distance is normalized and velocity amount comes only from \l magnitude and
92 \l magnitudeVariation.
93
94 The default value is \c false.
95
96 \sa magnitude, magnitudeVariation
97*/
98bool QQuick3DParticleTargetDirection::normalized() const
99{
100 return m_normalized;
101}
102
103void QQuick3DParticleTargetDirection::setNormalized(bool normalized)
104{
105 if (m_normalized == normalized)
106 return;
107
108 m_normalized = normalized;
109 Q_EMIT normalizedChanged();
110}
111
112/*!
113 \qmlproperty real TargetDirection3D::magnitude
114
115 This property defines the magnitude in position change per second. Negative magnitude
116 accelerates the opposite way from the \l {TargetDirection3D::position}{position}.
117 When the \l normalized is false, this is multiplied with the distance to the target position.
118
119 The default value is \c 1.0.
120
121 \sa magnitudeVariation
122*/
123float QQuick3DParticleTargetDirection::magnitude() const
124{
125 return m_magnitude;
126}
127
128void QQuick3DParticleTargetDirection::setMagnitude(float magnitude)
129{
130 if (qFuzzyCompare(p1: m_magnitude, p2: magnitude))
131 return;
132
133 m_magnitude = magnitude;
134 Q_EMIT magnitudeChanged();
135}
136
137/*!
138 \qmlproperty real TargetDirection3D::magnitudeVariation
139
140 This property defines the magnitude variation in position change per second.
141 When the \l normalized is false, this is multiplied with the distance to the target position.
142
143 The default value is \c 0.0.
144
145 \sa magnitude
146*/
147float QQuick3DParticleTargetDirection::magnitudeVariation() const
148{
149 return m_magnitudeVariation;
150}
151
152void QQuick3DParticleTargetDirection::setMagnitudeVariation(float magnitudeVariation)
153{
154 if (qFuzzyCompare(p1: m_magnitudeVariation, p2: magnitudeVariation))
155 return;
156
157 m_magnitudeVariation = magnitudeVariation;
158 Q_EMIT magnitudeChangedVariation();
159}
160
161QVector3D QQuick3DParticleTargetDirection::sample(const QQuick3DParticleData &d)
162{
163 QVector3D ret = m_position - d.startPosition;
164 if (!m_system)
165 return ret;
166 auto rand = m_system->rand();
167
168 ret.setX(ret.x() - m_positionVariation.x() + rand->get(particleIndex: d.index, user: QPRand::TDirPosXV) * m_positionVariation.x() * 2.0f);
169 ret.setY(ret.y() - m_positionVariation.y() + rand->get(particleIndex: d.index, user: QPRand::TDirPosYV) * m_positionVariation.y() * 2.0f);
170 ret.setZ(ret.z() - m_positionVariation.z() + rand->get(particleIndex: d.index, user: QPRand::TDirPosZV) * m_positionVariation.z() * 2.0f);
171 if (m_normalized)
172 ret.normalize();
173 ret *= (m_magnitude - m_magnitudeVariation + rand->get(particleIndex: d.index, user: QPRand::TDirMagV) * m_magnitudeVariation * 2.0f);
174 return ret;
175}
176
177QT_END_NAMESPACE
178

source code of qtquick3d/src/quick3dparticles/qquick3dparticletargetdirection.cpp