A generator is a function called once per particle
when a world starts, i.e.
function myGenerator(index) { ... }. It
must
return [size, position, velocity, mass]
: those 4 values, in that order, for every particle.
index is the position of the particle
currently being created (0, 1, 2, …), handy for
splitting particles into groups or spacing them out.
These parameters can be used to generate any
configuration of particles you want.
new Vector(x, y) : the first argument
is always x, the second is always y.
position is where the particle starts.
velocity is added to the particle's
position every single frame, so think of it as
"pixels moved per frame", not a real-world speed
unit.
rnd(), w and
h are not built into the language,
instead, each generator defines them itself at the
top, usually as
let rnd = () => Math.random(),
let w = window.innerWidth and
let h = window.innerHeight. Feel free
to change or drop them.
A wallGenerator(index) function works
like the particle generator but for straight wall
segments: it's called wallCount times
and must return [start, end] or
[start, end, anchorSize] : two Vectors
for the wall's endpoints, and an optional
anchor-point size for the third value. The anchor
point is a rigid particle added at the wall's
endpoints and intersections,
new World(particleCount, particleGenerator,
constrainX, constrainY, trail, wallCount,
wallGenerator, shape, frameRate)
particleCount : how many times the
generator runs at startup.
particleGenerator : your function
from above.
constrainX /
constrainY : see "Constraining each
side" below.
trail : trail length in pixels; 0
disables trails.
wallCount /
wallGenerator : how many explicit
interior walls to build, and the function that
generates them.
shape : optional. A URL to an
.svg file, or an inline
"<svg>...</svg>"
string, to auto-import as walls the moment this
world starts : exactly like using the "Import
SVG" button by hand.
frameRate : simulation speed in
frames per second (default 30).
constrainX controls the left and right
edges; constrainY controls top and
bottom. Each accepts:
"reflect" : bounces particles back
in, like a wall.
"wrap" : particles exiting one edge
reappear on the opposite edge.
"absorb" : particles that reach the
edge are deleted.
{ size, mass, velocity, rate, capacity
}
: spawns new particles inward from that edge at
a random point along it, at
rate particles/second, until
capacity have been emitted.
To give each of the 4 edges its own setting, pass an
object instead of a single value:
constrainX = { left: ..., right: ... }
and
constrainY = { top: ..., bottom: ... }.
Any value you'd normally use for the whole axis (a
string or an emit object) works as a per-side value
too.