Let’s talk about the world of particle systems in computer graphics today! In this blog post, we embark on an enlightening journey to understand the core concepts, applications, and real-world examples that make particle systems a cornerstone in the creation of breathtaking visual experiences.
This blog post is part of the Genuary 2024 series. It has a corresponding Github repository: ao-genuary/01-particles.
At its essence, a particle system is a dynamic simulation technique used in computer graphics to model and render complex, dynamic visual effects. Instead of treating visuals as static entities, particle systems breathe life into scenes by representing visual elements as individual particles. These particles move, interact, and evolve over time, collectively creating mesmerizing animations.
Let’s implement these key definitions of particle systems in computer graphics mentioned above and create a first simple example using Processing. (You can copy & paste this concept for any other programming language providing an interface to image creation and manipulation – Python & Cairo works just as well, for example). There’s very good example code for getting started with particle systems using Processing.
According to our key definitions above, we first need an object (or a data type) for the prototype for a particle. Using an object oriented approach, this would be a class named particle. A particle is the fundamental object of any particle system and has certain properties (for example position, shape, size, and color). An emitter would then create particles at a specific rate (for example, one particle per frame or second) and assign them specific attributes during emission (initial position, velocity, acceleration, color, lifetime, and shape for instance). These attributes are updated stepwise (per frame, per step, per time unit) by taking into account forces that modify particle behavior (acceleration, for example). The sum of all particles is our particle system. This particle system gets its own class ParticleSystem.
Let’s give it a try and start with the example for one particle system provided on the Processing website, the Simple Particle System and adapt it to our needs and play with it to learn more about particle systems in computer graphics.
We need to define what’s a particle first. As outlined above, we’re using an object oriented approach and define a particle class for this purpose. Particles need to have certain attributes: for example position, velocity, acceleration, lifetime, color, shape and so on. In our first example each particle has an initial position, velocity, acceleration, and lifespan.[1][2]
class Particle {
PVector pos; // position
PVector v; // velocity
PVector a; // acceleration
int life; // lifespan (generations)
}
Each instance of the particle object has to be constructed (emitted):
Particle(PVector p0) {
this.pos = p0.copy();
this.a = new PVector(random(-0.05, 0.05), random(-0.05, 0.05));
this.v = new PVector(random(-0.1, 0.01), random(-0.1, 0.1));
this.life = 255;
this.col = color(random(0,255), random(0,255), random(0,255));
}
The particle class has run, update and a display methods.
void run() {
update();
display();
}void update () {
this.v.add(this.a);
this.pos.add(this.v);
this.life -= 2;
}void display() {
stroke(col);
fill(this.col, this.life);
ellipse(this.pos.x, this.pos.y, this.life/4, this.life/4);
}
Both size and alpha (transparency) are dependent on the current lifetime and decrease from their start value towards zero. Particles are eliminated from the particle system (and the corresponding ArrayList, in this example) when its lifetime is smaller or equal to zero.
We create a particle system (let’s call it ps) and assign a starting position for its first particle to be added. The example mentioned above (the Simple Particle System) uses a dynamic array list (called ArrayList in Processing [2]) for storing the particles, which means you can add and remove particles on the fly and do not need to know how many particles there are going to be in the end. You could also use a basic array of particle objects (instances of our class Particle) instead, but then you’d need to know how many particles will be in your particle system at once, or limit the number of particles in one particle system to a fixed number (ten or hundred or a thousand, for instance).
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;}
The ParticleSystem class has a constructor too:
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
println(particles.size());
}
It also has methods for adding particles to the system, and a run() method including a check if a specific particle is still alive or not.
void addParticle(PVector pos) {
particles.add(new Particle(pos));
}void run() {
for (int i = particles.size()-1; i >= 0; i–) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
First run: particles emitted at the center of the picture, with random starting velocities, all with the same color and size attributes.
Secon run: same as above, but with added transparency according to each particle’s lifetime.
Added particle size, decreasing with lifetime.
Added some color.
[1] Double slashes (“//”) indicate comments in our code.
[2] We use Processing’s PVector data type to store three dimensional vectors, specifically Euclidean (also known as geometric) vectors.
[3] Not all programming languages provide variable length arrays. There are workarounds like using hashes, for example in Perl and Python. Eventually consult the reference of the programming language you’re using for further information on this topic.
As we wrap up this exploration, the incredible versatility and impact of particle systems in computer graphics become abundantly clear. From enhancing entertainment to facilitating scientific discovery, these dynamic simulations continue to push the boundaries of what is visually achievable, captivating audiences across various industries. The magic lies not just in the particles themselves, but in the limitless possibilities they unlock for creative minds in the ever-evolving landscape of computer graphics.
« Happy Accidents: Surprises & Generative PencilsGenerating Tartan Patterns »