Modelintro
Particle system
Also called Reeves particle system.
Model fuzzy phenomena such as fire, smoke and sparks as many point particles that are emitted with random attributes, moved by forces and removed when their lifetime ends.
A particle system models a fuzzy object, such as fire, smoke, sparks or spray, as a large set of point particles that are born, move and die. William Reeves introduced it in 1983 for the fire in the Genesis Demo sequence of the film Star Trek II. The model fixes the state of a particle and the rules of emission, update and death, and leaves the forces and the rendering to the user. It differs from the social force model, where the particles are pedestrians that push on each other, and from the mass-spring system, where springs join the particles: in Reeves's model the particles do not interact at all.
The machine
The state at frame is a set of particles. A particle carries a position (in m), a velocity (in m/s), a size, a color, a transparency, a shape and a remaining lifetime , which is a whole number of frames. A system also has an origin, an orientation and a generation shape: a sphere of radius , a circle of radius or a rectangle of length and width . Every random attribute is a mean plus a random part:
where is uniform in , is the mean and is what Reeves calls the variance (it is the half width of the range, not a statistical variance). Each frame does five steps (Reeves, Section 2):
- Emission. The number of new particles is with , a mean that changes linearly from the first frame of the system. A second form scales the count by the screen area of the system, so a small object on the screen gets few particles.
- Attributes. Each new particle gets a random position inside the generation shape. A sphere sends particles outward from its center; a circle or a rectangle sends them up, tilted by at most an ejection angle. The speed is . Color, transparency, size and lifetime use the same rule.
- Death. A particle with is removed. Optional rules also remove a particle whose intensity falls below a threshold, or one that has moved too far from the origin in a given direction.
- Update. Each remaining particle moves by its velocity, its velocity changes by a constant acceleration (for example gravity m/s), its color, transparency and size change at fixed rates, and decreases by 1.
- Rendering. Each living particle is drawn, in Reeves's renderer as a point light source added to the frame buffer.
Reeves adds the velocity to the position once per frame and does not state the order of the two updates. This page and its code use symplectic Euler, velocity first, with a frame time :
One frame, without the rendering, is then:
PARTICLE-SYSTEM-FRAME(P, f, dt)- 1
mean <- N0 + dN (f - f0) - 2
n <- round(mean + Rand() VarN) ▷ Rand() is uniform in [-1, 1] - 3
repeat n times: add to P a particle with a position in the generation shape, speed MeanSpeed + Rand() VarSpeed and lifetime MeanLife + Rand() VarLife - 4
remove from P each particle with lifetime 0 or outside the region of interest - 5
for each particle in P: v <- v + dt a - 6
x <- x + dt v - 7
lifetime <- lifetime - 1
The standing assumptions are that particles do not act on each other and do not intersect the other objects of the scene (Reeves composites those objects in a separate pass), and that each particle is drawn as a point light source, so the light of the particles adds up and no particle hides another. The code with this page is 2D, with , up, and a segment as the generation shape.
What it counts
The model gives the positions and the appearance of all living particles at each frame, and so an image of the fuzzy object. The quantities that the designer controls are statistical: the mean rate of birth, the spread of speeds and directions, and the mean lifetime. The model makes no claim about the physics of fire or smoke. It is a procedural model: the parameters are tuned until the result looks right, and each one is a tuning, not a measured property of a material.
The cost of a frame is for living particles, because every step treats each particle once and no step looks at pairs. Memory is .
Relation to other models
The particle system is the simplest of the particle models in graphics. The others add an interaction:
- A mass-spring system joins particles with springs, so the forces depend on pairs of positions. Cloth and soft bodies use it.
- Smoothed particle hydrodynamics gives each particle a density and a pressure from its neighbors, so the particles behave as a fluid.
- Boids and the social force model give each particle steering rules or forces from its neighbors, for flocks and crowds.
- Molecular dynamics moves atoms under pair potentials, with the same update loop but with physical forces and units.
- Position-based dynamics moves particles and then projects them onto constraints.
Each of these keeps the particle state of Reeves and replaces step 4 by a solver with pair forces or constraints. The birth and death rules are what stay specific to particle systems for effects.
Known results in the model
- No forces, straight lines (proved). With , the update gives exactly, in exact arithmetic. The test checks this to in floating point.
- Free fall (proved, closed form). A particle dropped from rest under gravity with symplectic Euler has after frames, and with explicit Euler (position first) . The exact value is with . For s and , symplectic Euler gives m, explicit Euler gives m, and the exact value is m. Both errors are , so both methods are first order. The test checks the three numbers.
- Lifetime in frames (proved, for the update order above). A particle born with lifetime is moved and drawn in exactly frames. With births per frame and no variance, the count after frame (counted from 0) is . The test checks this.
- Mean number of particles (proved). If every frame has the same mean birth count and the lifetimes are independent with mean , and no other rule kills particles, the expected number of living particles in the steady state is , by linearity of expectation over the birth frames. This is Little's law for the particle population.
- Range of the birth count (proved). With rounding to the nearest integer, lies in , and its mean is up to the rounding. The test checks the range and measures the mean over 400 frames with a fixed seed.
- Parallel cost (observed). Because no step looks at pairs, every step is a map over the particles. Sims (1990) wrote the operations as data parallel functions and ran the update and the rendering on a data parallel computer; his timings are measurements on that machine.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/particle-system/. The authors are listed in prov/8JJ5P6.jsonl.
"""A 2D particle system with the emission, update and death rules of Reeves 1983.
Each frame does the steps of Reeves, Section 2: generate new particles, give
each one its attributes, remove the particles whose lifetime is over, and move
the others. Rendering is left out. Every random attribute is a mean plus
Rand() times a variance, where Rand() is uniform in [-1, 1] (Reeves, Sections
2.1 and 2.2). The motion uses symplectic Euler (velocity first) with a constant
acceleration; Reeves adds the acceleration to the velocity and the velocity to
the position once per frame, and does not fix the order, so the order here is
our choice. Positions are (x, y) with y up, in metres.
"""
import math
GRAVITY = (0.0, -9.81)
def rand_pm1(rng):
"""Reeves's Rand(): a uniform number in [-1, 1]."""
return 2.0 * rng.random() - 1.0
class Params:
"""The parameters of one particle system. Each pair (mean, var) gives the range mean +- var."""
def __init__(self, mean_parts=10.0, delta_mean_parts=0.0, var_parts=0.0,
radius=0.0, ejection=0.0, mean_speed=1.0, var_speed=0.0,
mean_life=10, var_life=0, accel=GRAVITY, floor=None):
self.mean_parts = mean_parts
self.delta_mean_parts = delta_mean_parts
self.var_parts = var_parts
self.radius = radius # generation shape: segment of half width radius on the x axis
self.ejection = ejection # largest angle from the vertical, in radians
self.mean_speed = mean_speed
self.var_speed = var_speed
self.mean_life = mean_life # lifetime in frames
self.var_life = var_life
self.accel = accel
self.floor = floor # region of interest: kill particles with y < floor
class Particle:
__slots__ = ("x", "v", "life", "ident")
def __init__(self, x, v, life, ident):
self.x = x
self.v = v
self.life = life
self.ident = ident
class ParticleSystem:
def __init__(self, params, origin=(0.0, 0.0), f0=0):
self.p = params
self.origin = origin
self.f0 = f0
self.frame = f0
self.particles = []
self.next_ident = 0
def emit_count(self, rng):
p = self.p
mean = p.mean_parts + p.delta_mean_parts * (self.frame - self.f0) # @line L1
return max(0, int(math.floor(mean + rand_pm1(rng) * p.var_parts + 0.5))) # @line L2
def new_particle(self, rng):
p = self.p
x = (self.origin[0] + rand_pm1(rng) * p.radius, self.origin[1])
angle = rand_pm1(rng) * p.ejection
speed = p.mean_speed + rand_pm1(rng) * p.var_speed
v = (speed * math.sin(angle), speed * math.cos(angle))
life = max(1, int(math.floor(p.mean_life + rand_pm1(rng) * p.var_life + 0.5)))
part = Particle(x, v, life, self.next_ident) # @line L3
self.next_ident += 1
return part
def step(self, dt, rng):
"""Compute one frame and return the number of particles born in it."""
n = self.emit_count(rng)
for _ in range(n):
self.particles.append(self.new_particle(rng))
floor = self.p.floor
self.particles = [q for q in self.particles # @line L4
if q.life > 0 and (floor is None or q.x[1] >= floor)]
ax, ay = self.p.accel
for q in self.particles:
q.v = (q.v[0] + dt * ax, q.v[1] + dt * ay) # @line L5
q.x = (q.x[0] + dt * q.v[0], q.x[1] + dt * q.v[1]) # @line L6
q.life -= 1 # @line L7
self.frame += 1
return n
def alive(self):
return len(self.particles)
def state(self):
return [(q.ident, q.x, q.v, q.life) for q in self.particles]
def free_fall(y0, steps, dt, g=9.81, order="symplectic"):
"""Height of one particle dropped from rest. order is "symplectic" or "explicit"."""
y, v = y0, 0.0
for _ in range(steps):
if order == "symplectic":
v -= dt * g
y += dt * v
else:
y += dt * v
v -= dt * g
return y
test_particle_system.py 123 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/particle-system/. The authors are listed in prov/8JJ5P6.jsonl.
"""Tests of the Reeves particle system: emission, update and death with fixed seeds."""
import random
from particle_system import Params, ParticleSystem, free_fall
def run(seed, frames, params, dt=1.0 / 30.0):
rng = random.Random(seed)
ps = ParticleSystem(params)
for _ in range(frames):
ps.step(dt, rng)
return ps
def busy_params():
return Params(mean_parts=12.0, var_parts=5.0, radius=0.2, ejection=0.4,
mean_speed=5.0, var_speed=1.0, mean_life=20, var_life=6, floor=-1.0)
def test_same_seed_same_state():
a = run(7, 60, busy_params()).state()
b = run(7, 60, busy_params()).state()
c = run(8, 60, busy_params()).state()
assert a == b
assert a != c
def test_emission_count_in_range_and_mean():
p = Params(mean_parts=10.0, delta_mean_parts=0.5, var_parts=4.0, mean_life=1)
rng = random.Random(3)
ps = ParticleSystem(p, f0=0)
total = 0
frames = 400
for f in range(frames):
n = ps.step(0.01, rng)
mean = 10.0 + 0.5 * f
assert mean - 4.0 - 0.5 <= n <= mean + 4.0 + 0.5
total += n - mean
# Rand() has mean 0, so the average excess is near 0 (a measurement with a fixed seed).
assert abs(total / frames) < 0.3
def test_lifetime_in_frames():
# With no variance, m particles are born per frame and each lives L frames.
m, life = 3, 5
p = Params(mean_parts=float(m), mean_life=life, accel=(0.0, 0.0))
rng = random.Random(1)
ps = ParticleSystem(p)
seen = {}
for f in range(12):
ps.step(0.1, rng)
assert ps.alive() == m * min(f + 1, life)
for ident, _, _, _ in ps.state():
seen[ident] = seen.get(ident, 0) + 1
born_early = [i for i in seen if i < m * (12 - life)]
assert born_early and all(seen[i] == life for i in born_early)
def test_random_lifetimes_bounded():
p = busy_params()
p.floor = None
rng = random.Random(11)
ps = ParticleSystem(p)
frames_alive = {}
for _ in range(80):
ps.step(1.0 / 30.0, rng)
for ident, _, _, _ in ps.state():
frames_alive[ident] = frames_alive.get(ident, 0) + 1
finished = [k for k in frames_alive if k < 300]
assert all(14 <= frames_alive[k] <= 26 for k in finished)
def test_floor_kills():
# A particle that falls below the floor in one frame is removed in the next one.
p = Params(mean_parts=5.0, var_parts=2.0, mean_speed=1.0, mean_life=1000, floor=-1.0)
rng = random.Random(2)
ps = ParticleSystem(p)
below = set()
killed = 0
for _ in range(60):
ps.step(0.05, rng)
ids = {q.ident for q in ps.particles}
assert not (below & ids)
killed += len(below)
below = {q.ident for q in ps.particles if q.x[1] < -1.0}
assert killed > 0
def test_free_fall_check_value():
p = Params(mean_parts=1.0, mean_speed=0.0, mean_life=100)
rng = random.Random(0)
ps = ParticleSystem(p)
ps.step(0.1, rng)
p.mean_parts = 0.0
for _ in range(9):
ps.step(0.1, rng)
(q,) = ps.particles
assert abs(q.x[1] - (-5.3955)) < 1e-12
assert abs(free_fall(0.0, 10, 0.1) - (-5.3955)) < 1e-12
assert abs(free_fall(0.0, 10, 0.1, order="explicit") - (-4.4145)) < 1e-12
assert abs(-0.5 * 9.81 * 1.0 ** 2 - (-4.905)) < 1e-12
def test_straight_lines_without_forces():
p = Params(mean_parts=4.0, var_parts=2.0, ejection=1.0, mean_speed=2.0, var_speed=1.0,
mean_life=30, accel=(0.0, 0.0))
rng = random.Random(5)
ps = ParticleSystem(p)
ps.step(0.1, rng)
start = {q.ident: (q.x, q.v) for q in ps.particles}
p.mean_parts = 0.0
p.var_parts = 0.0
for _ in range(10):
ps.step(0.1, rng)
for q in ps.particles:
x0, v0 = start[q.ident]
assert q.v == v0
assert abs(q.x[0] - (x0[0] + 1.0 * v0[0])) < 1e-12
assert abs(q.x[1] - (x0[1] + 1.0 * v0[1])) < 1e-12
References
- William T. Reeves, Particle systems: a technique for modeling a class of fuzzy objects, ACM Transactions on Graphics 2(2), 91-108, 1983 (also Computer Graphics 17(3), SIGGRAPH 83, 359-376), Section 2 (the five steps of a frame), Section 2.1 (the number of new particles), Section 2.2 (the attributes and the generation shape), Section 2.3 (dynamics) and Section 2.4 (extinction)
- Karl Sims, Particle animation and rendering using data parallel computation, Proceedings of SIGGRAPH 90, 405-413, 1990, the whole paper (particle operations written as data parallel functions over all particles, general locator)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 8JJ5P6 Edit this page History Report an error Markdown