Skip to content

Instantly share code, notes, and snippets.

@01010111
Created January 28, 2021 21:09
Show Gist options
  • Save 01010111/525e9e591e43f2dbed92a775dd0394ed to your computer and use it in GitHub Desktop.
Save 01010111/525e9e591e43f2dbed92a775dd0394ed to your computer and use it in GitHub Desktop.
Bloomin' Particles
import zero.utilities.Vec2;
import h2d.Graphics;
using zero.utilities.EventBus;
class LineParticles extends Graphics {
var lines:Array<LineData> = [];
public function new(parent) {
super(parent);
update.listen('update');
}
public function add(line:LineData) {
if (line.acceleration == null) line.acceleration = Vec2.get();
if (line.last == null) line.last = line.pos.copy();
if (line.drag == null) line.drag = 1;
if (line.alpha == null) line.alpha = 1;
lines.push(line);
}
function update(?dt) {
clear();
var to_remove = [];
for (line in lines) {
line.pos.x += line.velocity.x * dt;
line.pos.y += line.velocity.y * dt;
line.velocity.x += line.acceleration.x * dt;
line.velocity.y += line.acceleration.y * dt;
line.velocity *= line.drag;
line.acceleration *= line.drag;
lineStyle(line.width, line.color, line.alpha);
moveTo(line.last.x, line.last.y);
lineTo(line.pos.x, line.pos.y);
line.last = line.last + (line.pos - line.last) * 0.25;
if (!verify(line)) to_remove.push(line);
}
for (line in to_remove) {
line.velocity.put();
line.acceleration.put();
line.pos.put();
line.last.put();
lines.remove(line);
}
}
function verify(line:LineData) {
if (line.last.x < 0 || line.last.x > Main.width) return false;
if (line.last.y < 0 || line.last.y > Main.height) return false;
if (line.velocity.length < 0.1) return false;
return true;
}
}
typedef LineData = {
width:Int,
color:Int,
pos:Vec2,
velocity:Vec2,
?last:Vec2,
?acceleration:Vec2,
?drag:Float,
?alpha:Float,
}
import h2d.Object;
import hxd.Window;
import h2d.filter.Bloom;
using zero.utilities.EventBus;
using zero.extensions.Tools;
class Main extends hxd.App {
static function main() new Main();
public static var width:Float;
public static var height:Float;
public static var particles:LineParticles;
override function init() {
width = Window.getInstance().width;
height = Window.getInstance().height;
s2d.filter = new Bloom(4.2, 10, 128, 3, 1);
particles = new LineParticles(s2d);
}
override function update(dt:Float) {
super.update(dt);
'update'.dispatch(dt);
}
}

Bloomin particles

To use this you gotta install zerolib haxelib install zerolib and add it to your build.hxml -lib zerolib

Main.hx

Here I'm just doing a handful of things

  • exposing width and height (I'm assuming there's some standard way of doing this in heaps but I'm lazy
  • throwing a bloom filter onto the stage (s2d)
  • creating an instance of line particles
  • in update() I'm using my EventBus utility to dispatch an update signal with the delta time

LineParticles.hx

This does a few things:

  • it keeps an array of active line particles
  • you can instantiate a particle with add()
  • it's just a graphics object that clears itself and draws active line particles every frame
  • lines are removed from the array if they leave the screen or if their length is small enough

The lines look rad bc of the bloom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment