1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dparticlegravity_p.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \qmltype Gravity3D
10 \inherits Affector3D
11 \inqmlmodule QtQuick3D.Particles3D
12 \brief Accelerates particles to a vector of the specified magnitude in the specified direction.
13 \since 6.2
14
15 This element models the gravity of a massive object whose center of gravity is far away (and thus the
16 gravitational pull is effectively constant across the scene). To model the gravity of an object near
17 or inside the scene, use \l Attractor3D.
18*/
19
20QQuick3DParticleGravity::QQuick3DParticleGravity(QQuick3DNode *parent)
21 : QQuick3DParticleAffector(parent)
22{
23}
24
25/*!
26 \qmlproperty real Gravity3D::magnitude
27
28 This property defines the magnitude in position change per second. Negative magnitude
29 accelerates the opposite way from the \l {Gravity3D::direction}{direction}.
30
31 The default value is \c 100.0.
32*/
33float QQuick3DParticleGravity::magnitude() const
34{
35 return m_magnitude;
36}
37
38void QQuick3DParticleGravity::setMagnitude(float magnitude)
39{
40 if (qFuzzyCompare(p1: m_magnitude, p2: magnitude))
41 return;
42
43 m_magnitude = magnitude;
44 Q_EMIT magnitudeChanged();
45 Q_EMIT update();
46}
47
48/*!
49 \qmlproperty vector3d Gravity3D::direction
50
51 This property defines the direction the gravity will affect toward. Values will be
52 automatically normalized to a unit vector.
53
54 The default value is \c (0.0, -1.0, 0.0) (downwards).
55*/
56const QVector3D &QQuick3DParticleGravity::direction() const
57{
58 return m_direction;
59}
60
61void QQuick3DParticleGravity::setDirection(const QVector3D &direction)
62{
63 if (m_direction == direction)
64 return;
65
66 m_direction = direction;
67 m_directionNormalized = m_direction.normalized();
68 Q_EMIT directionChanged();
69 Q_EMIT update();
70}
71
72void QQuick3DParticleGravity::affectParticle(const QQuick3DParticleData &sd, QQuick3DParticleDataCurrent *d, float time)
73{
74 Q_UNUSED(sd);
75 float velocity = 0.5f * m_magnitude * (time * time);
76 d->position += velocity * m_directionNormalized;
77}
78
79QT_END_NAMESPACE
80

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