Skip to content

Particle System

粒子系统是算法艺术中最基础也是最强大的技术之一。

基础概念

粒子系统由大量独立的小元素组成,每个元素都有自己的属性:

  • 位置 — 粒子在空间中的坐标
  • 速度 — 粒子移动的方向和速率
  • 加速度 — 影响粒子运动的力
  • 生命周期 — 粒子的存在时间
  • 外观 — 粒子的颜色、大小、透明度等

代码示例

javascript
class Particle {
  constructor(x, y) {
    this.pos = createVector(x, y);
    this.vel = p5.Vector.random2D().mult(random(1, 3));
    this.acc = createVector(0, 0);
    this.life = 255;
    this.size = random(2, 8);
  }

  applyForce(force) {
    this.acc.add(force);
  }

  update() {
    this.vel.add(this.acc);
    this.pos.add(this.vel);
    this.acc.mult(0);
    this.life -= 2;
  }

  display() {
    noStroke();
    fill(255, this.life);
    ellipse(this.pos.x, this.pos.y, this.size);
  }

  isDead() {
    return this.life <= 0;
  }
}

应用场景

  • 火焰、烟雾、爆炸特效
  • 星空、自然景观模拟
  • 数据可视化
  • 交互式艺术装置

扩展阅读