1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtQml/qqmlinfo.h>
41#include "qquickgravity_p.h"
42#include <cmath>
43QT_BEGIN_NAMESPACE
44const qreal CONV = 0.017453292520444443;
45/*!
46 \qmltype Gravity
47 \instantiates QQuickGravityAffector
48 \inqmlmodule QtQuick.Particles
49 \ingroup qtquick-particles
50 \inherits Affector
51 \brief For applying acceleration in an angle.
52
53 This element will accelerate all affected particles to a vector of
54 the specified magnitude in the specified angle. If the angle and acceleration do
55 not vary, it is more efficient to set the specified acceleration on the Emitter.
56
57 This element models the gravity of a massive object whose center of
58 gravity is far away (and thus the gravitational pull is effectively constant
59 across the scene). To model the gravity of an object near or inside the scene,
60 use PointAttractor.
61*/
62
63/*!
64 \qmlproperty real QtQuick.Particles::Gravity::magnitude
65
66 Pixels per second that objects will be accelerated by.
67*/
68void QQuickGravityAffector::setMagnitude(qreal arg)
69{
70 if (m_magnitude != arg) {
71 m_magnitude = arg;
72 m_needRecalc = true;
73 emit magnitudeChanged(arg);
74 }
75}
76
77qreal QQuickGravityAffector::magnitude() const
78{
79 return m_magnitude;
80}
81
82
83/*!
84 \qmlproperty real QtQuick.Particles::Gravity::acceleration
85 \deprecated
86
87 \warning The name for this property has changed to magnitude, use it instead.
88*/
89void QQuickGravityAffector::setAcceleration(qreal arg)
90{
91 qmlWarning(me: this) << "The acceleration property is deprecated. Please use magnitude instead.";
92 setMagnitude(arg);
93}
94
95/*!
96 \qmlproperty real QtQuick.Particles::Gravity::angle
97
98 Angle of acceleration.
99*/
100void QQuickGravityAffector::setAngle(qreal arg)
101{
102 if (m_angle != arg) {
103 m_angle = arg;
104 m_needRecalc = true;
105 emit angleChanged(arg);
106 }
107}
108
109qreal QQuickGravityAffector::angle() const
110{
111 return m_angle;
112}
113
114QQuickGravityAffector::QQuickGravityAffector(QQuickItem *parent) :
115 QQuickParticleAffector(parent), m_magnitude(-10), m_angle(90), m_needRecalc(true)
116{
117}
118
119bool QQuickGravityAffector::affectParticle(QQuickParticleData *d, qreal dt)
120{
121 if (!m_magnitude)
122 return false;
123 if (m_needRecalc) {
124 m_needRecalc = false;
125 m_dx = m_magnitude * std::cos(x: m_angle * CONV);
126 m_dy = m_magnitude * std::sin(x: m_angle * CONV);
127 }
128
129 d->setInstantaneousVX(vx: d->curVX(particleSystem: m_system) + m_dx*dt, particleSystem: m_system);
130 d->setInstantaneousVY(vy: d->curVY(particleSystem: m_system) + m_dy*dt, particleSystem: m_system);
131 return true;
132}
133
134
135
136QT_END_NAMESPACE
137
138#include "moc_qquickgravity_p.cpp"
139

source code of qtdeclarative/src/particles/qquickgravity.cpp