aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3dparticles/qquick3dparticlegravity.cpp
blob: 1df77152fcb7d3a7a7d8bdf2ee0eee6caaa5c188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dparticlegravity_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype Gravity3D
    \inherits Affector3D
    \inqmlmodule QtQuick3D.Particles3D
    \brief Accelerates particles to a vector of the specified magnitude in the specified direction.
    \since 6.2

    This element models the gravity of a massive object whose center of gravity is far away (and thus the
    gravitational pull is effectively constant across the scene). To model the gravity of an object near
    or inside the scene, use \l Attractor3D.
*/

QQuick3DParticleGravity::QQuick3DParticleGravity(QQuick3DNode *parent)
    : QQuick3DParticleAffector(parent)
{
}

/*!
    \qmlproperty real Gravity3D::magnitude

    This property defines the magnitude in position change per second. Negative magnitude
    accelerates the opposite way from the \l {Gravity3D::direction}{direction}.

    The default value is \c 100.0.
*/
float QQuick3DParticleGravity::magnitude() const
{
    return m_magnitude;
}

void QQuick3DParticleGravity::setMagnitude(float magnitude)
{
    if (qFuzzyCompare(m_magnitude, magnitude))
        return;

    m_magnitude = magnitude;
    Q_EMIT magnitudeChanged();
    Q_EMIT update();
}

/*!
    \qmlproperty vector3d Gravity3D::direction

    This property defines the direction the gravity will affect toward. Values will be
    automatically normalized to a unit vector.

    The default value is \c (0.0, -1.0, 0.0) (downwards).
*/
const QVector3D &QQuick3DParticleGravity::direction() const
{
    return m_direction;
}

void QQuick3DParticleGravity::setDirection(const QVector3D &direction)
{
    if (m_direction == direction)
        return;

    m_direction = direction;
    m_directionNormalized = m_direction.normalized();
    Q_EMIT directionChanged();
    Q_EMIT update();
}

void QQuick3DParticleGravity::affectParticle(const QQuick3DParticleData &sd, QQuick3DParticleDataCurrent *d, float time)
{
    Q_UNUSED(sd);
    float velocity = 0.5f * m_magnitude * (time * time);
    d->position += velocity * m_directionNormalized;
}

QT_END_NAMESPACE