Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicktargetdirection.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
6#include <cmath>
7#include <QDebug>
8#include <QRandomGenerator>
9
51 QQuickDirection(parent)
52 , m_targetX(0)
53 , m_targetY(0)
54 , m_targetVariation(0)
55 , m_proportionalMagnitude(false)
56 , m_magnitude(0)
57 , m_magnitudeVariation(0)
58 , m_targetItem(nullptr)
59{
60}
61
63{
64 //###This approach loses interpolating the last position of the target (like we could with the emitter) is it worthwhile?
68 if (m_targetItem){
69 QQuickParticleEmitter* parentEmitter = qobject_cast<QQuickParticleEmitter*>(parent());
70 targetX = m_targetItem->width()/2;
71 targetY = m_targetItem->height()/2;
72 if (!parentEmitter){
73 qWarning() << "Directed vector is not a child of the emitter. Mapping of target item coordinates may fail.";
74 targetX += m_targetItem->x();
75 targetY += m_targetItem->y();
76 }else{
77 ret = parentEmitter->mapFromItem(m_targetItem, QPointF(targetX, targetY));
78 targetX = ret.x();
79 targetY = ret.y();
80 }
81 }else{
82 targetX = m_targetX;
83 targetY = m_targetY;
84 }
85 targetX += 0 - from.x() - m_targetVariation + QRandomGenerator::global()->generateDouble() * m_targetVariation*2;
86 targetY += 0 - from.y() - m_targetVariation + QRandomGenerator::global()->generateDouble() * m_targetVariation*2;
87 qreal theta = std::atan2(targetY, targetX);
88 qreal mag = m_magnitude + QRandomGenerator::global()->generateDouble() * m_magnitudeVariation * 2 - m_magnitudeVariation;
89 if (m_proportionalMagnitude)
90 mag *= qHypot(targetX, targetY);
91 ret.setX(mag * std::cos(theta));
92 ret.setY(mag * std::sin(theta));
93 return ret;
94}
95
97
98#include "moc_qquicktargetdirection_p.cpp"
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:72
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:73
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
QQuickTargetDirection(QObject *parent=nullptr)
\qmltype TargetDirection \instantiates QQuickTargetDirection \inqmlmodule QtQuick....
QPointF sample(const QPointF &from) override
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
Combined button and popup list for selecting options.
auto qHypot(qfloat16 x, qfloat16 y)
Definition qfloat16.h:443
#define qWarning
Definition qlogging.h:166
return ret
double qreal
Definition qtypes.h:187
QObject::connect nullptr