Qt 6.x
The Qt SDK
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
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:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
Q_INVOKABLE QPointF mapFromItem(const QQuickItem *item, const QPointF &point) const
Maps the given point in item's coordinate system to the equivalent point within this item's coordinat...
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:73
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:74
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
QQuickTargetDirection(QObject *parent=nullptr)
\qmltype TargetDirection \instantiates QQuickTargetDirection \inqmlmodule QtQuick....
QPointF sample(const QPointF &from) override
double generateDouble()
Generates one random qreal in the canonical range [0, 1) (that is, inclusive of zero and exclusive of...
Definition qrandom.h:58
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:397
#define qWarning
Definition qlogging.h:162
return ret
double qreal
Definition qtypes.h:92
QObject::connect nullptr
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent