ReferenceBehaviorsFunction
drag()
Slows particles down in proportion to their speed.
function drag(rate: Sampler<number>, edgeRatio?: number): Behavior;Creates a behavior that applies air resistance proportional to a particle’s speed.
Every second, a particle loses the fraction rate of its velocity. A fast particle therefore slows down sharply
at first, then more and more gently. Together with gravity(), a falling particle speeds up
until it reaches its terminal velocity of gravity / rate, so if you know how fast particles should fall,
use drag(gravity / terminalVelocity). The rate is sampled once for each particle, when it spawns.
Flat particles like paper feel more resistance when they move face-on than when they move edge-on. Set
edgeRatio below 1 to simulate this: a tilted particle then glides sideways while it falls, and as it tumbles,
the glide reverses, so it sways back and forth. The face is the plane of the particle’s shape, so this needs
particles that spin, for example with randomSpin(). On average over all orientations,
the drag stays at rate, so the terminal velocity stays close to gravity / rate.
Parameters
Section titled “Parameters”rateSampler<number>The fraction of its velocity a particle loses per second, in 1/s. Values between 2 and 10 are usually sensible. Sampled for each particle.
edgeRatiooptionalnumberdefault1How much drag a particle feels when it moves edge-on, relative to face-on. 1 makes drag the same in every direction. Values between 0.3 and 0.5 give a paper-like flutter, while lower values make particles glide far to the side. Defaults to 1.
Returns
Section titled “Returns”Example
Section titled “Example”// Falls at most 160px/s under gravity(800), and flutters like paper.const behaviors = [gravity(800), drag(800 / 160, 0.4)];