Move your pointer to move the emitter.

0:01 / 0:30
Playback speed

Particles

Move the emitter suddenly. Follow one spark that was already in the air when you moved.

Interactive demo#MotionAndFeelABZÛPS2 era

Lots of tiny short-lived sprites that together read as sparks, smoke or dust.

An emitter spits out particles, each with a random direction, speed and lifetime. Each one follows simple rules, here gravity and fading, and then disappears. The effect comes from the crowd, not from any single particle.

Code

src/demos/particles.goose
namespace particles_lesson;
import stage;
import std;
import math;

struct Particle { x: f64, y: f64, vx: f64, vy: f64, life: f64 }
var particles: Particle[>..] = [];
fn advance(x: f64, y: f64, dt: f64) {
    for i in 3 { particles.push(Particle { x, y, random_unit() * 60 - 30, -50 - random_unit() * 50, 1.0 }); }
    var alive = 0;
    for p in particles {
        var q = p;
        q.vy += 170 * dt;
        q.x += q.vx * dt;
        q.y += q.vy * dt;
        q.life -= dt * 0.9;
        if q.life > 0 {
            particles[alive] = q;
            alive += 1;
        }
    }
    particles.pop_n(particles.len - alive);
}

export "particles" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    artwork("cave");
    let x = if hover { px } else { 80 + sin(time) * 30 };
    let y = if hover { py } else { 66.0 };
    if dt > 0 {
        advance(x, y, dt);
    }
    else if particles.len == 0 { for i in 40 { advance(x, y, 0.03); } }
    draw_prop("crystal", x - 6, y - 4, 12, 16);
    for p in particles {
        opacity(clamp(p.life * 1.5, 0.0, 1.0));
        let size = 0.5 + 1.2 * p.life;
        quad(p.x, p.y - size, p.x + size * 0.55, p.y, p.x, p.y + size, p.x - size * 0.55, p.y,
             if p.life > 0.65 { WHITE } else if p.life > 0.35 { SUN } else { MAG });
    }
    opacity(1);
    ring(x, y, 5, LINE, 2, 2);
    cross(floor(x + 0.5), floor(y + 0.5), WHITE, 2);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.