Qt Quick パーティクルの例 - システム
これは、QML パーティクルシステムにおけるアフェクターの使用例を集めたものです。

これは、パーティクルシステムにおけるAffectorの使用に関連する、簡単なQMLのサンプル集です。各サンプルは、特定のタイプや機能を強調した小さなQMLファイルです。
「動的比較」では、パーティクルシステムを使用する場合と、Image タイプを動的にインスタンス化する以下のコードで同様の効果を得る場合とを比較しています。
Item {
id: fakeEmitter
function burst(number) {
while (number > 0) {
let item = fakeParticle.createObject(root)
item.lifeSpan = Math.random() * 5000 + 5000
item.x = Math.random() * (root.width / 2) + (root.width / 2)
item.y = 0
number--
}
}
Component {
id: fakeParticle
Image {
id: container
property int lifeSpan: 10000
width: 32
height: 32
source: "qrc:///particleresources/glowdot.png"
PropertyAnimation on y { from: -16; to: root.height - 16; duration: container.lifeSpan; running: true }
SequentialAnimation on opacity {
running: true
NumberAnimation { from: 0; to: 1; duration: 500 }
PauseAnimation { duration: container.lifeSpan - 1000 }
NumberAnimation { from: 1; to: 0; duration: 500 }
ScriptAction { script: container.destroy(); }
}
}
}
}Imageオブジェクトにはランダムに色を付けることができない点に注意してください。
「Start」と「Stop」は、ParticleSystem の再生状態と一時停止状態を設定するだけです。システムは停止または一時停止中はシミュレーションを実行しませんが、「restart」を実行するとシミュレーションが最初から再開され、「unpause」を実行すると中断した箇所からシミュレーションが再開されます。
「Timed group changes」は、ParticleGroup タイプを際立たせる例です。通常は文字列名でグループを参照するだけで十分ですが、グループにプロパティを設定することで、さらなる効果を実現できます。最初のグループには可変の持続時間が設定されていますが、常に 2 番目のグループへと遷移します。
ParticleGroup {
name: "fire"
duration: 2000
durationVariation: 2000
to: {"splode":1}
}2番目のグループにはTrailEmitter が設定されており、3番目のグループへパーティクルを放出する固定の持続時間が設定されています。TrailEmitter をParticleGroup の直接の子として配置することで、追従するグループとして自動的にそのグループが選択されます。
ParticleGroup {
name: "splode"
duration: 400
to: {"dead":1}
TrailEmitter {
group: "works"
emitRatePerParticle: 100
lifeSpan: 1000
maximumEmitted: 1200
size: 8
velocity: AngleDirection {angle: 270; angleVariation: 45; magnitude: 20; magnitudeVariation: 20;}
acceleration: PointDirection {y:100; yVariation: 20}
}
}3番目のグループには、Affectorが直接の子として配置されており、これによりAffectorはこのグループを自動的にターゲットにします。Affectorを使用すると、パーティクルがこのグループに入るとすぐに、そのパーティクルのx、y座標を使用して、別のエミッターに対してバースト関数を呼び出すことができます。
ParticleGroup {
name: "dead"
duration: 1000
Affector {
once: true
onAffected: (x, y)=> worksEmitter.burst(400,x,y)
}
}TrailEmitter が複数のエミッターに対する要件に合わない場合は、同じParticleSystem とイメージパーティクルを使い続けながら、動的にエミッターを作成することも可能です。
for (var i = 0; i < 8; i++) {
let obj = emitterComp.createObject(root)
obj.x = x
obj.y = y
obj.targetX = Math.random() * 240 - 120 + obj.x
obj.targetY = Math.random() * 240 - 120 + obj.y
obj.life = Math.round(Math.random() * 2400) + 200
obj.emitRate = Math.round(Math.random() * 32) + 32
obj.go()
}なお、このエフェクト(飛び交う虹色の槍の群れ)は、「TrailEmitter 」を使用した方が適しています。この例では、概念をより簡単に示すために、動的エミッターを使用しているだけです。
「Multiple Painters」では、個々のパーティクルのペイント順序を制御する方法を示しています。1つのImagePainter内のパーティクルのペイント順序は厳密には定義されていませんが、ImageParticle オブジェクトは、 Qt Quick アイテムの通常のZ順序ルールに従います。この例では、同じ論理パーティクルの異なる部分をそれぞれ描画する2つのImageParticleを使用して、黒い境界線より上のパーティクルの内部を描画することができます。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.