1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dparticlepointrotator_p.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \qmltype PointRotator3D
10 \inherits Affector3D
11 \inqmlmodule QtQuick3D.Particles3D
12 \brief Rotates particles around a pivot point.
13 \since 6.2
14
15 This element rotates particles around a \l pivotPoint towards the \l direction.
16*/
17
18QQuick3DParticlePointRotator::QQuick3DParticlePointRotator(QQuick3DNode *parent)
19 : QQuick3DParticleAffector(parent)
20{
21}
22
23/*!
24 \qmlproperty real PointRotator3D::magnitude
25
26 This property defines the magnitude in degrees per second. Negative magnitude
27 rotates to the opposite way from the \l {PointRotator3D::direction}{direction}.
28
29 The default value is \c 10.0.
30*/
31float QQuick3DParticlePointRotator::magnitude() const
32{
33 return m_magnitude;
34}
35
36void QQuick3DParticlePointRotator::setMagnitude(float magnitude)
37{
38 if (qFuzzyCompare(p1: m_magnitude, p2: magnitude))
39 return;
40
41 m_magnitude = magnitude;
42 Q_EMIT magnitudeChanged();
43 Q_EMIT update();
44}
45
46/*!
47 \qmlproperty vector3d PointRotator3D::direction
48
49 This property defines the direction for the rotation. Values will be
50 automatically normalized to a unit vector.
51
52 The default value is \c (0.0, 1.0, 0.0) (around the y-coordinate).
53*/
54QVector3D QQuick3DParticlePointRotator::direction() const
55{
56 return m_direction;
57}
58
59void QQuick3DParticlePointRotator::setDirection(const QVector3D &direction)
60{
61 if (m_direction == direction)
62 return;
63
64 m_direction = direction;
65 m_directionNormalized = m_direction.normalized();
66 Q_EMIT directionChanged();
67 Q_EMIT update();
68}
69
70/*!
71 \qmlproperty vector3d PointRotator3D::pivotPoint
72
73 This property defines the pivot point for the rotation. Particles are rotated
74 around this point.
75
76 The default value is \c (0, 0, 0) (the center of particle system).
77*/
78QVector3D QQuick3DParticlePointRotator::pivotPoint() const
79{
80 return m_pivotPoint;
81}
82
83void QQuick3DParticlePointRotator::setPivotPoint(const QVector3D &point)
84{
85 if (m_pivotPoint == point)
86 return;
87
88 m_pivotPoint = point;
89 Q_EMIT pivotPointChanged();
90 Q_EMIT update();
91}
92
93void QQuick3DParticlePointRotator::prepareToAffect()
94{
95 m_rotationMatrix.setToIdentity();
96 m_rotationMatrix.translate(vector: m_pivotPoint);
97}
98
99void QQuick3DParticlePointRotator::affectParticle(const QQuick3DParticleData &sd, QQuick3DParticleDataCurrent *d, float time)
100{
101 // Rotate based on the current position
102 // Note that this means order of PointRotator element compared to other affectors matters
103 Q_UNUSED(sd);
104 if (!qFuzzyIsNull(f: m_magnitude)) {
105 QMatrix4x4 rot = m_rotationMatrix;
106 rot.rotate(angle: time * m_magnitude, vector: m_directionNormalized);
107 rot.translate(vector: -m_pivotPoint);
108 d->position = rot.map(point: d->position);
109 }
110}
111
112QT_END_NAMESPACE
113

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