<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- affectors.qdoc --> <title>Qt Quick Particles Examples - Affectors | Qt Quick 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td >Qt Quick Particles Examples - Affectors</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt Quick Particles Examples - Affectors</h1> <span class="subtitle"></span> <!-- $$$particles/affectors-brief --> <p>This is a collection of examples using Affectors in the QML particle system.</p> <!-- @@@particles/affectors --> <!-- $$$particles/affectors-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/qml-affectors-example.png" alt="" /></p><p>This is a collection of small QML examples relating to using Affectors in the particle system. Each example is a small QML file emphasizing a particular type or feature.</p> <p>Age demonstrates using an Age affector to prematurely end the lives of particles.</p> <pre class="qml"> Age { anchors.fill: parent system: particles once: true lifeLeft: 1200 advancePosition: false } </pre> <p>As you move the affector around the screen, the particles inside it (which haven't already been affected) jump to a period near the end of their life. This gives them a short period to finish fading out, but changing lifeLeft to 0 (the default), would cause them to reach the end of their life instantly.</p> <p>Attractor demonstrates using an Attractor affector to simulate a black hole</p> <pre class="qml"> Attractor { id: gs; pointX: root.width/2; pointY: root.height/2; strength: 4000000; affectedParameter: Attractor.Acceleration proportionalToDistance: Attractor.InverseQuadratic } </pre> <p>All particles in the scene, including the rocket ship's exhaust and pellets, are pulled towards the black hole. This effect is stronger closer to the black hole, so the asteroids near the top of the screen are barely affected at all, while the ones towards the middle sometimes curve drastically. To complete the effect, an Age affector covers the black hole to destroy particles which come in contact with it.</p> <p>Custom Affector manipulates the properties of the particles directly in javascript. One Affector is used to make the leaves rock back and forth as they fall, looking more leaf-like than just spinning in circles:</p> <pre class="qml"> Affector { property real coefficient: 0.1 property real velocity: 1.5 width: parent.width height: parent.height - 100 onAffectParticles: { /* //Linear movement if (particle.r == 0) { particle.r = Math.random() > 0.5 ? -1 : 1; } else if (particle.r == 1) { particle.rotation += velocity * dt; if (particle.rotation >= maxAngle) particle.r = -1; } else if (particle.r == -1) { particle.rotation -= velocity * dt; if (particle.rotation <= -1 * maxAngle) particle.r = 1; } */ //Wobbly movement for (var i=0; i<particles.length; i++) { var particle = particles[i]; if (particle.r == 0.0) { particle.r = Math.random() + 0.01; } particle.rotation += velocity * particle.r * dt; particle.r -= particle.rotation * coefficient; if (particle.r == 0.0) particle.r -= particle.rotation * 0.000001; particle.update = 1; } } } </pre> <p>Another is used to provide a slightly varying friction to the leaves as they 'land', to look more natural:</p> <pre class="qml"> Affector {//Custom Friction, adds some 'randomness' x: -60 width: parent.width + 120 height: 100 anchors.bottom: parent.bottom onAffectParticles: { for (var i=0; i<particles.length; i++) { var particle = particles[i]; var pseudoRand = (Math.floor(particle.t*1327) % 10) + 1; var yslow = dt * pseudoRand * 0.5 + 1; var xslow = dt * pseudoRand * 0.05 + 1; if (particle.vy < 1) particle.vy = 0; else particle.vy = (particle.vy / yslow); if (particle.vx < 1) particle.vx = 0; else particle.vx = (particle.vx / xslow); particle.update = true; } } } </pre> <p>Friction is similar to the falling leaves in the custom affector, except that it uses a flat friction the whole way down instead of custom affectors.</p> <pre class="qml"> Friction { anchors.fill: parent anchors.margins: -40 factor: 0.4 } </pre> <p>Gravity is a convenience affector for applying a constant acceleration to particles inside it</p> <pre class="qml"> Gravity { system: sys magnitude: 32 angle: ground.rotation + 90 } </pre> <p><a href="qml-qtquick-particles-groupgoal.html">GroupGoal</a> sets up two particle groups for flaming and non-flaming balls, and gives you various ways to transition between them.</p> <pre class="qml"> ParticleGroup { name: "unlit" duration: 1000 to: {"lighting":1, "unlit":99} ImageParticle { source: "../../images/particleA.png" colorVariation: 0.1 color: "#2060160f" } GroupGoal { whenCollidingWith: ["lit"] goalState: "lighting" jump: true } } </pre> <p>The non-flaming balls have a one in a hundred chance of lighting on their own each second, but they also have a <a href="qml-qtquick-particles-groupgoal.html">GroupGoal</a> set on the whole group. This affector affects all particles of the unlit group, when colliding with particles in the lit group, and cause them to move to the lighting group.</p> <pre class="qml"> ParticleGroup { name: "lighting" duration: 100 to: {"lit":1} } </pre> <p>lighting is an intermediate group so that the glow builds up and the transition is less jarring. So it automatically moves into the lit group after 100ms.</p> <pre class="qml"> ParticleGroup { name: "lit" duration: 10000 onEntered: score++; TrailEmitter { id: fireballFlame group: "flame" emitRatePerParticle: 48 lifeSpan: 200 emitWidth: 8 emitHeight: 8 size: 24 sizeVariation: 8 endSize: 4 } TrailEmitter { id: fireballSmoke group: "smoke" </pre> <p>The lit group also has TrailEmitters on it for additional fire and smoke, but does not transition anywhere. There are two more <a href="qml-qtquick-particles-groupgoal.html">GroupGoal</a> objects that allow particles in the unlit group to transition to the lighting group (and then to the lit group).</p> <pre class="qml"> GroupGoal { groups: ["unlit"] goalState: "lit" jump: true system: particles x: -15 y: -55 height: 75 width: 30 shape: MaskShape {source: "../../images/matchmask.png"} } </pre> <p>The first is just an area bound to the location of an image of a pilot flame. When unlit balls pass through the flame, they go straight to lit because the pilot flame is so hot.</p> <pre class="qml"> //Click to enflame GroupGoal { groups: ["unlit"] goalState: "lighting" jump: true enabled: ma.pressed width: 18 height: 18 x: ma.mouseX - width/2 y: ma.mouseY - height/2 } </pre> <p>The second is bound to the location of the last pointer interaction, so that touching or clicking on unlit balls (which is hard due to their constant movement) causes them to move to the lighting group.</p> <p>Move shows some simple effects you can get by altering trajectory midway. The red particles have an affector that affects their position, jumping them forwards by 120px.</p> <pre class="qml"> Affector { groups: ["A"] x: 120 width: 80 height: 80 once: true position: PointDirection { x: 120; } } </pre> <p>The green particles have an affector that affects their velocity, but with some angle variation. By adding some random direction velocity to their existing forwards velocity, they begin to spray off in a cone.</p> <pre class="qml"> Affector { groups: ["B"] x: 120 y: 240 width: 80 height: 80 once: true velocity: AngleDirection { angleVariation:360; magnitude: 72 } } </pre> <p>The blue particles have an affector that affects their acceleration, and because it sets relative to false this resets the acceleration instead of adding to it. Once the blue particles reach the affector, their horizontal velocity stops increasing as their vertical velocity decreases.</p> <pre class="qml"> Affector { groups: ["C"] x: 120 y: 400 width: 80 height: 120 once: true relative: false acceleration: PointDirection { y: -80; } } </pre> <p><a href="qml-qtquick-particles-spritegoal.html">SpriteGoal</a> has an affector which interacts with the sprite engine of particles, if they are being drawn as sprites by <a href="qml-qtquick-particles-imageparticle.html">ImageParticle</a>.</p> <pre class="qml"> SpriteGoal { groups: ["meteor"] system: sys goalState: "explode" jump: true anchors.fill: rocketShip width: 60 height: 60 } </pre> <p>The <a href="qml-qtquick-particles-spritegoal.html">SpriteGoal</a> follows the image of the rocket ship on screen, and when it interacts with particles drawn by <a href="qml-qtquick-particles-imageparticle.html">ImageParticle</a> as sprites, it instructs them to move immediately to the "explode" state, which in this case is the animation of the asteroid breaking into many pieces.</p> <p>Turbulence has a flame with smoke, and both sets of particles being affected by a Turbulence affector. This gives a faint wind effect.</p> <pre class="qml"> Turbulence { id: turb enabled: true height: (parent.height / 2) - 4 width: parent.width x: parent. width / 4 anchors.fill: parent strength: 32 NumberAnimation on strength{from: 16; to: 64; easing.type: Easing.InOutBounce; duration: 1800; loops: -1} } </pre> <p>To make the wind change direction, subsitute a black and white noise image in the noiseSource parameter (it currently uses a default noise source).</p> <p>Wander uses a Wander affector to add some horizontal drift to snowflakes as they fall down.</p> <pre class="qml"> Wander { id: wanderer system: particles anchors.fill: parent xVariance: 360/(wanderer.affectedParameter+1); pace: 100*(wanderer.affectedParameter+1); } </pre> <p>There are different movements given by applying the Wander to different attributes of the trajectory, so the example makes it easy to play around and see the difference.</p> <p>Files:</p> <ul> <li><a href="qtquick-particles-affectors-affectors-pro.html">particles/affectors/affectors.pro</a></li> <li><a href="qtquick-particles-affectors-affectors-qml.html">particles/affectors/affectors.qml</a></li> <li><a href="qtquick-particles-affectors-affectors-qmlproject.html">particles/affectors/affectors.qmlproject</a></li> <li><a href="qtquick-particles-affectors-affectors-qrc.html">particles/affectors/affectors.qrc</a></li> <li><a href="qtquick-particles-affectors-content-greybutton-qml.html">particles/affectors/content/GreyButton.qml</a></li> <li><a href="qtquick-particles-affectors-content-age-qml.html">particles/affectors/content/age.qml</a></li> <li><a href="qtquick-particles-affectors-content-attractor-qml.html">particles/affectors/content/attractor.qml</a></li> <li><a href="qtquick-particles-affectors-content-customaffector-qml.html">particles/affectors/content/customaffector.qml</a></li> <li><a href="qtquick-particles-affectors-content-friction-qml.html">particles/affectors/content/friction.qml</a></li> <li><a href="qtquick-particles-affectors-content-gravity-qml.html">particles/affectors/content/gravity.qml</a></li> <li><a href="qtquick-particles-affectors-content-groupgoal-qml.html">particles/affectors/content/groupgoal.qml</a></li> <li><a href="qtquick-particles-affectors-content-move-qml.html">particles/affectors/content/move.qml</a></li> <li><a href="qtquick-particles-affectors-content-spritegoal-qml.html">particles/affectors/content/spritegoal.qml</a></li> <li><a href="qtquick-particles-affectors-content-turbulence-qml.html">particles/affectors/content/turbulence.qml</a></li> <li><a href="qtquick-particles-affectors-content-wander-qml.html">particles/affectors/content/wander.qml</a></li> <li><a href="qtquick-particles-affectors-main-cpp.html">particles/affectors/main.cpp</a></li> </ul> </div> <!-- @@@particles/affectors --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> 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. </p> </div> </body> </html>