Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add emitter position to particles to place the emitter relative to the local origin #517

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/particletrail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @ts-check

kaplay();

loadSprite("hexagon", "./examples/sprites/particle_hexagon_filled.png");

onLoad(() => {
const trail = add([
pos(),
particles({
max: 20,
speed: [200, 250],
lifeTime: [0.2, 0.75],
colors: [WHITE],
opacities: [1.0, 0.0],
angle: [0, 360],
texture: getSprite("hexagon").data.tex,
quads: [getSprite("hexagon").data.frames[0]],
}, {
rate: 5,
direction: -90,
spread: 2,
})
])

onMouseMove((pos, delta) => {
trail.emitter.position = pos;
trail.emit(1);
})
})
31 changes: 24 additions & 7 deletions src/components/draw/particles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type EmitterOpt = {
* Rate of emission in particles per second if the emitter should emit out of itself.
*/
rate?: number;
/**
* Position (relative) of emission.
*/
position: Vec2;
/**
* Direction of emission.
*/
Expand Down Expand Up @@ -125,6 +129,16 @@ export type ParticlesOpt = {
* @group Component Types
*/
export interface ParticlesComp extends Comp {
emitter: {
/**
* Relative position of the emitter
*/
position: Vec2;
/**
* Relative direction of the emitter
*/
direction: number;
}
/**
* Emit a number of particles
*/
Expand All @@ -144,7 +158,6 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {
const quads = popt.quads || [new Quad(0, 0, 1, 1)];
const scales = popt.scales || [1];
const lifetime = popt.lifeTime;
const direction = eopt.direction || 0;
const spread = eopt.spread || 0;
const speed = popt.speed || [0, 0];
const angleRange = popt.angle || [0, 0];
Expand Down Expand Up @@ -192,6 +205,10 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {

return {
id: "particles",
emitter: {
position: eopt.position || vec2(),
direction: eopt.direction || 0,
},
emit(n: number) {
n = Math.min(n, popt.max - count);
let index: number | null = 0;
Expand All @@ -200,8 +217,8 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {
if (index == null) return;

const velocityAngle = rand(
direction - spread,
direction + spread,
this.emitter.direction - spread,
this.emitter.direction + spread,
);
const vel = Vec2.fromAngle(velocityAngle).scale(
rand(speed[0], speed[1]),
Expand All @@ -220,9 +237,9 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {
dampingRange[1],
);
const lt = lifetime ? rand(lifetime[0], lifetime[1]) : null;
const pos = eopt.shape
const pos = this.emitter.position.add(eopt.shape
? eopt.shape.random()
: vec2();
: vec2());

const p = particles[index];
p.t = 0;
Expand Down Expand Up @@ -270,10 +287,10 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {
time += DT;
while (
count < popt.max && eopt.rate
&& time > eopt.rate
&& time > 1 / eopt.rate
) {
this.emit(1);
time -= eopt.rate;
time -= 1 / eopt.rate;
}
},
draw() {
Expand Down