Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qquickfriction.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
"qquickfriction_p.h"
5
#
include
<
qmath
.
h
>
6
7
QT_BEGIN_NAMESPACE
8
/*!
9
\qmltype Friction
10
\nativetype QQuickFrictionAffector
11
\inqmlmodule QtQuick.Particles
12
\ingroup qtquick-particles
13
\inherits ParticleAffector
14
\brief For applying friction proportional to the particle's current velocity.
15
16
*/
17
18
/*!
19
\qmlproperty real QtQuick.Particles::Friction::factor
20
21
A drag will be applied to moving objects which is this factor of their current velocity.
22
*/
23
/*!
24
\qmlproperty real QtQuick.Particles::Friction::threshold
25
26
The drag will only be applied to objects with a velocity above the threshold velocity. The
27
drag applied will bring objects down to the threshold velocity, but no further.
28
29
The default threshold is 0
30
*/
31
static
qreal
sign
(qreal a)
32
{
33
return
a >= 0 ? 1 : -1;
34
}
35
36
static
const
qreal
epsilon
= 0.00001;
37
38
QQuickFrictionAffector::QQuickFrictionAffector(QQuickItem *parent) :
39
QQuickParticleAffector(parent), m_factor(0.0), m_threshold(0.0)
40
{
41
}
42
43
bool
QQuickFrictionAffector::affectParticle(QQuickParticleData *d, qreal dt)
44
{
45
if
(!m_factor)
46
return
false
;
47
qreal curVX = d->curVX(m_system);
48
qreal curVY = d->curVY(m_system);
49
if
(!curVX && !curVY)
50
return
false
;
51
qreal newVX = curVX + (curVX * m_factor * -1 * dt);
52
qreal newVY = curVY + (curVY * m_factor * -1 * dt);
53
54
if
(!m_threshold) {
55
if
(sign(curVX) != sign(newVX))
56
newVX = 0;
57
if
(sign(curVY) != sign(newVY))
58
newVY = 0;
59
}
else
{
60
qreal curMag = qSqrt(curVX*curVX + curVY*curVY);
61
if
(curMag <= m_threshold + epsilon)
62
return
false
;
63
qreal newMag = qSqrt(newVX*newVX + newVY*newVY);
64
if
(newMag <= m_threshold + epsilon ||
//went past the threshold, stop there instead
65
sign(curVX) != sign(newVX) ||
//went so far past maybe it came out the other side!
66
sign(curVY) != sign(newVY)) {
67
qreal theta = qAtan2(curVY, curVX);
68
newVX = m_threshold * qCos(theta);
69
newVY = m_threshold * qSin(theta);
70
}
71
}
72
73
d->setInstantaneousVX(newVX, m_system);
74
d->setInstantaneousVY(newVY, m_system);
75
return
true
;
76
}
77
QT_END_NAMESPACE
78
79
#
include
"moc_qquickfriction_p.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
epsilon
static const qreal epsilon
Definition
qquickfriction.cpp:36
sign
static QT_BEGIN_NAMESPACE qreal sign(qreal a)
\qmltype Friction \nativetype QQuickFrictionAffector \inqmlmodule QtQuick.Particles\inherits Particle...
Definition
qquickfriction.cpp:31
qtdeclarative
src
particles
qquickfriction.cpp
Generated on
for Qt by
1.18.0