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