Skip to content

1681-engine / index / ParticleSim

Class: ParticleSim ​

Defined in: src/particles/ParticleSim.ts:10

The particles of one emitter as flat typed arrays (structure of arrays): position, velocity, age and lifetime, rotation and spin, and a per-particle random draw the curves can use. Pure, no Pixi: the emitter draws what this holds, and tests step it under node.

Live particles are packed at indices 0 … count - 1. A particle that dies is replaced by the last live one, so a step never leaves holes and never allocates; the order of particles is therefore not stable, which drawing does not need (every live index is written again each frame).

Constructors ​

Constructor ​

ts
new ParticleSim(capacity): ParticleSim;

Defined in: src/particles/ParticleSim.ts:23

Parameters ​

ParameterType
capacitynumber

Returns ​

ParticleSim

Properties ​

age ​

ts
readonly age: Float32Array;

Defined in: src/particles/ParticleSim.ts:15


capacity ​

ts
readonly capacity: number;

Defined in: src/particles/ParticleSim.ts:23


count ​

ts
count: number = 0;

Defined in: src/particles/ParticleSim.ts:21


life ​

ts
readonly life: Float32Array;

Defined in: src/particles/ParticleSim.ts:16


rot ​

ts
readonly rot: Float32Array;

Defined in: src/particles/ParticleSim.ts:17


seed ​

ts
readonly seed: Float32Array;

Defined in: src/particles/ParticleSim.ts:20

A random draw per particle (0–1), fixed for its life: size variance, flicker phase.


spin ​

ts
readonly spin: Float32Array;

Defined in: src/particles/ParticleSim.ts:18


vx ​

ts
readonly vx: Float32Array;

Defined in: src/particles/ParticleSim.ts:13


vy ​

ts
readonly vy: Float32Array;

Defined in: src/particles/ParticleSim.ts:14


x ​

ts
readonly x: Float32Array;

Defined in: src/particles/ParticleSim.ts:11


y ​

ts
readonly y: Float32Array;

Defined in: src/particles/ParticleSim.ts:12

Methods ​

clear() ​

ts
clear(): number;

Defined in: src/particles/ParticleSim.ts:86

Every particle gone; returns how many there were.

Returns ​

number


progress() ​

ts
progress(i): number;

Defined in: src/particles/ParticleSim.ts:81

The share of its life particle i has lived (0–1).

Parameters ​

ParameterType
inumber

Returns ​

number


spawn() ​

ts
spawn(
   x, 
   y, 
   vx, 
   vy, 
   life, 
   rot, 
   spin, 
   seed
): number;

Defined in: src/particles/ParticleSim.ts:36

Adds a particle; returns its index, or -1 when the sim is full. life is in seconds.

Parameters ​

ParameterType
xnumber
ynumber
vxnumber
vynumber
lifenumber
rotnumber
spinnumber
seednumber

Returns ​

number


step() ​

ts
step(
   dt, 
   gravityX, 
   gravityY, 
   drag
): number;

Defined in: src/particles/ParticleSim.ts:56

Advances every particle by dt seconds: gravity (px/s²) added to velocity, then drag, then position. Drag is the share of speed lost per second, applied as exp(-drag × dt) so it is the same at any frame rate. Returns how many particles died this step.

Parameters ​

ParameterType
dtnumber
gravityXnumber
gravityYnumber
dragnumber

Returns ​

number

Released under the MIT License.